15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

XMl <strong>and</strong> aDo.neT ❘ 927<br />

To summarize, the key thing to keep in mind when performing transforms is to remember to use the proper<br />

XML data store. Use XPathDocument if you do not need editing capabilities, XmlDataDocument if you are<br />

getting your data from ADO.<strong>NET</strong>, <strong>and</strong> XmlDocument if you need to be able to edit the data. In each case,<br />

you are dealing with the same process.<br />

Xml <strong>and</strong> ado.neT<br />

XML is the glue that binds ADO.<strong>NET</strong> to the rest of the world. ADO.<strong>NET</strong> was designed from the ground up<br />

to work within the XML environment. XML is used to transfer the data to <strong>and</strong> from the data store <strong>and</strong> the<br />

application or web page. Because ADO.<strong>NET</strong> uses XML as the transport in remoting scenarios, data can be<br />

exchanged with applications <strong>and</strong> systems that are not even aware of ADO.<strong>NET</strong>. Because of the importance of<br />

XML in ADO.<strong>NET</strong>, some powerful features in ADO.<strong>NET</strong> allow the reading <strong>and</strong> writing of XML documents.<br />

The System.Xml namespace also contains classes that can consume or utilize ADO.<strong>NET</strong> relational data.<br />

The database that is used for the examples is from the AdventureWorksLT sample application. The sample<br />

database can be downloaded from codeplex.com/SqlServerSamples. Note that there are several versions<br />

of the AdventureWorks database. Most will work, but the LT version is the simplified version <strong>and</strong> is more<br />

than adequate for the purposes of this chapter.<br />

Converting ado.neT data to Xml<br />

The first example uses ADO.<strong>NET</strong>, streams, <strong>and</strong> XML to pull some data from the database into a DataSet,<br />

load an XmlDocument object with the XML from the DataSet, <strong>and</strong> load the XML into a text box. To run<br />

the next few examples, you need to add the following using statements:<br />

using System.Data;<br />

using System.Xml;<br />

using System.Data.SqlClient;<br />

using System.IO;<br />

The connection string is defined as a module-level variable:<br />

string _connectString = "Server=.\\SQLExpress;<br />

Database=adventureworkslt;Trusted_Connection=Yes";<br />

The ADO.<strong>NET</strong> samples have a DataGrid object added to the forms. This will allow you to see the data<br />

in the ADO.<strong>NET</strong> DataSet because it is bound to the grid, as well as the data from the generated XML<br />

documents that you load in the text box. Here is the code for the first example. The first step in the<br />

examples is to create the st<strong>and</strong>ard ADO.<strong>NET</strong> objects to produce a DataSet object. After the data set has<br />

been created, it is bound to the grid.<br />

private void button1_Click(object sender, EventArgs e)<br />

{<br />

XmlDocument doc = new XmlDocument();<br />

DataSet ds = new DataSet("XMLProducts");<br />

SqlConnection conn = new SqlConnection(_connectString);<br />

SqlDataAdapter da = new SqlDataAdapter<br />

("SELECT Name, St<strong>and</strong>ardCost FROM SalesLT.Product", conn);<br />

//fill the dataset<br />

da.Fill(ds, "Products");<br />

//load data into grid<br />

dataGridView1.DataSource = ds.Tables["Products"];<br />

code snippet frmADOXML.cs<br />

After you create the ADO.<strong>NET</strong> objects <strong>and</strong> bind to the grid, you instantiate a MemoryStream object, a<br />

StreamReader object, <strong>and</strong> a StreamWriter object. The StreamReader <strong>and</strong> StreamWriter objects will use<br />

the MemoryStream to move the XML around:<br />

MemoryStream memStrm=new MemoryStream();<br />

StreamReader strmRead=new StreamReader(memStrm);<br />

StreamWriter strmWrite=new StreamWriter(memStrm);<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!