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.

928 ❘ ChaPTer 33 mAnipulAtinG xml<br />

You use a MemoryStream so that you don’t have to write anything to disk; however, you could have used<br />

any object that was based on the Stream class, such as FileStream.<br />

This next step is where the XML is generated. You call the WriteXml() method from the DataSet<br />

class. This method generates an XML document. WriteXml() has two overloads: one takes a string with<br />

the file path <strong>and</strong> name, <strong>and</strong> the other adds a mode parameter. This mode is an XmlWriteMode enumeration,<br />

with the following possible values:<br />

➤<br />

➤<br />

➤<br />

IgnoreSchema<br />

WriteSchema<br />

DiffGram<br />

IgnoreSchema is used if you do not want WriteXml() to write an inline schema at the start of your XML<br />

file; use the WriteSchema parameter if you do want one. A DiffGram shows the data before <strong>and</strong> after an<br />

edit in a DataSet.<br />

//write the xml from the dataset to the memory stream<br />

ds.WriteXml(strmWrite, XmlWriteMode.IgnoreSchema);<br />

memStrm.Seek(0, SeekOrigin.Begin);<br />

//read from the memory stream to a XmlDocument object<br />

doc.Load(strmRead);<br />

//get all of the products elements<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 />

Figure 33-5 shows the data in the list as well as the bound data grid.<br />

code snippet frmADOXML.cs<br />

figure 33-5<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!