13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

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

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

}root.AppendChild(n);n.InnerText = "Content of "+ d.Name;Figure 5-5 demonstrates the structure of the newly created <strong>XML</strong> file.Figure 5-5: An <strong>XML</strong> file represent<strong>in</strong>g a directory list<strong>in</strong>g created us<strong>in</strong>g the <strong>XML</strong> DOM API.Persist<strong>in</strong>g ChangesThe f<strong>in</strong>al step <strong>in</strong> sav<strong>in</strong>g the <strong>XML</strong> document we have created is to attach the node to the rest of the document and save the document, as shown here:doc.AppendChild(root);doc.Save(fileName);To persist all the changes to a storage medium, you call the Save method, whichconta<strong>in</strong>s four overloads, shown here:public virtual void Save(Stream);public virtual void Save(str<strong>in</strong>g);public virtual void Save(TextWriter);public virtual void Save(XmlWriter);The <strong>XML</strong> document can be saved to a disk file as well as to an output stream, <strong>in</strong>clud<strong>in</strong>gnetwork and compressed streams. You can also <strong>in</strong>tegrate the class that manages thedocument with other .<strong>NET</strong> Framework applications by us<strong>in</strong>g writers, and you cancomb<strong>in</strong>e more <strong>XML</strong> documents us<strong>in</strong>g, <strong>in</strong> particular, <strong>XML</strong> writers.Whatever overload you choose, it is always an <strong>XML</strong> writer that does the job ofpersist<strong>in</strong>g <strong>XML</strong> nodes to a storage medium. The XmlDocument class makes use of aspecialized version of the XmlTextWriter class that simply works around one of thelimitations of <strong>XML</strong> writers.<strong>XML</strong> writers do not allow you to write element and attribute nodes <strong>for</strong> which you have aprefix but an empty namespace. If the namespace URI is set to null, the writersuccessfully looks up the closest def<strong>in</strong>ition <strong>for</strong> that prefix and figures out thenamespace, if one exists. If the namespace is simply an empty str<strong>in</strong>g, however, anArgumentException exception is thrown. The <strong>XML</strong> DOM <strong>in</strong>ternal writer overrides theWriteStartElement and WriteStartAttribute methods. If the namespace URI is emptywhen the prefix is not, the new overrides reset the prefix to the empty str<strong>in</strong>g and noexception is raised.193

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

Saved successfully!

Ooh no, something went wrong!