15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Working with Different XMl objects ❘ 943<br />

Prior to the LINQ to XML release, working with XML using System.Xml was not an easy task. With the<br />

inclusion of System.Xml.Linq , you now fi nd a series of capabilities that make the process of working with<br />

XML in your code much easier.<br />

Many developers previously turned to the XmlDocument object to create XML within their application<br />

code. This object allows you to create XML documents that enable you to append elements, attributes, <strong>and</strong><br />

other items in a hierarchical fashion. With LINQ to XML <strong>and</strong> the inclusion of the new System.Xml.Linq<br />

namespace, you will now fi nd some new objects that make the creation of XML documents a much<br />

simpler process.<br />

WorKing WiTh differenT Xml objeCTs<br />

In addition to the LINQ querying ability included in .<strong>NET</strong> 4, the .<strong>NET</strong> Framework includes XML objects<br />

that work so well they can st<strong>and</strong> on their own outside of LINQ. You can use these objects in place of<br />

working directly with the DOM in this release. Within the System.Xml.Linq namespace, you will fi nd a<br />

series of LINQ to XML helper objects that make working with an XML document in memory<br />

much easier.<br />

The following sections work through the objects that are available to you within this namespace.<br />

Many of the examples in this chapter use a fi le called Hamlet.xml. You can fi nd this<br />

fi l e a t http://metalab.unc.edu/bosak/xml/eg/shaks200.zip that includes all of<br />

Shakespeare’s plays as XML fi les.<br />

X d ocument<br />

The XDocument is a replacement of the XmlDocument object from the pre - .<strong>NET</strong> 3.5 world; it is easier to<br />

work with in dealing with XML documents. The XDocument object works with the other new objects in this<br />

space, such as the XNamespace , XComment , XElement , <strong>and</strong> XAttribute objects.<br />

One of the more important members of the XDocument object is the Load() method:<br />

XDocument xdoc = XDocument.Load(@"C:\Hamlet.xml");<br />

This operation will load the Hamlet.xml contents as an in - memory XDocument object. You can also pass<br />

a TextReader or XmlReader object into the Load() method. From here, you are able to programmatically<br />

work with the XML:<br />

XDocument xdoc = XDocument.Load(@"C:\Hamlet.xml");<br />

Console.WriteLine(xdoc.Root.Name.ToString());<br />

Console.WriteLine(xdoc.Root.HasAttributes.ToString());<br />

This produces the following results:<br />

PLAY<br />

False<br />

code download ConsoleApplication1.sln<br />

Another important member to be aware of is the Save() method, which, like the Load() method, allows<br />

you to save to a physical disk location or to a TextWriter or XmlWriter object:<br />

XDocument xdoc = XDocument.Load(@"C:\Hamlet.xml");<br />

xdoc.Save(@"C:\CopyOfHamlet.xml");<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!