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 ❘ 931<br />

SqlDataAdapter daProduct = new SqlDataAdapter<br />

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

SqlDataAdapter daCategory = new SqlDataAdapter<br />

("SELECT ProductCategoryID, Name from SalesLT.ProductCategory", conn);<br />

//Fill DataSet from both SqlAdapters<br />

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

daCategory.Fill(ds, "Categories");<br />

//Add the relation<br />

ds.Relations.Add(ds.Tables["Categories"].Columns["ProductCategoryID"],<br />

ds.Tables["Products"].Columns["ProductCategoryID"]);<br />

//Write the Xml to a file so we can look at it later<br />

ds.WriteXml("Products.xml", XmlWriteMode.WriteSchema);<br />

//load data into grid<br />

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

//create the XmlDataDocument<br />

doc = new XmlDataDocument(ds);<br />

//Select the productname elements <strong>and</strong> load them in the grid<br />

XmlNodeList nodeLst = doc.SelectNodes("//XMLProducts/Products");<br />

textBox1.Text = "";<br />

foreach (XmlNode node in nodeLst)<br />

{<br />

textBox1.Text += node.InnerXml + "\r\n";<br />

}<br />

}<br />

code snippet frmADOXML.cs<br />

In the sample you are creating, there are two DataTables in the XMLProducts DataSet: Products <strong>and</strong><br />

Categories. You create a new relation on the ProductCategoryID column in both tables.<br />

By using the same WriteXml() method call that you did in the previous example, you will get the following<br />

XML file (SuppProd.xml):<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!