LINQ To XML: working with documents

In the previous posts I”ve presented some of the basic features introduced by the new LINQ To XML API. Today, we”ll keep digging and I”ll show you several options available for working with XML documents. As we”ve seen in the other day, documents are represented by instances of the XDocument class. You”ll really need to use a XDocument when you need to load a DTD or when you need to add a processing instruction. Lets suppose you need to apply a XSL stylesheet to your XML tree. To achieve this, you need to create a new XDocument and then pass it an instance of the XProcessingInstruction. Here”s how you can achieve that:

var xml = new XDocument(
                       new XProcessingInstruction(“xml-stylesheet”, “type=”text/xsl” href=”mySheet.xsl””),
                       new XElement(“clients”,
                           new XElement(“client”,
                               new XElement(“name”, “Luis”)
                           )
                       )
                   );

Not really hard…Another thing you might need is setting the encoding of the current XML document. For instance, take a look at the following code:

var xml = new XDocument(
                        new XDeclaration(“1.0”, “utf-8”, “yes”),
                        new XElement(“clients”,
                            new XElement(“client”,
                                new XElement(“name”, “Luis”)
                            )
                        )
                    );

Btw, if you”re running this code on a console and you pass the xml local variable to the Console.WriteLine method, you won”t see the xml declaration because the XNode class overrides the ToString method and explictly states that the xml declaration should be omitted (it does this by setting the OmitXmlDeclaration property of the XmlWriterSettings to true before passing it to the XmlWriter that is used to write the content of the node). The important thing to keep in mind is that, even though you don”t see it (when you pass it to Console.WriteLine), it”s really there.

Another thing which I end up doing a lot when working with XML is loading documents. Thankfully, the XDocument class makes this really easy. Don”t believe me? ok, just take a look at all the overloads of the static Load method defined by the class:

public static XDocument Load(TextReader textReader);
public static XDocument Load(string uri);
public static XDocument Load(XmlReader reader);
public static XDocument Load(TextReader textReader, LoadOptions options);
public static XDocument Load(string uri, LoadOptions options);
public static XDocument Load(XmlReader reader, LoadOptions options);

Say you have a file called myFile.xml on the D: folder. you can load it by using the following code:

var doc = XDocument.Load( “d:myFile.xml”);

And yes, you do use the same method to load xml from a web site. For instance, the following code loads my RSS feed:

var doc = XDocument.Load(http://feeds.feedburner.com/laneten);

Internally, the method uses the XmlReader class for loading the XML. You can also control the settings (XmlReaderSettings) used by the XmlReader by using the overload which receives a LoadOptions flag enumeration (internally, the class will build a XmlReaderSettings from the LoadOptions you pass in). With this enumeration, you can specify:

  • if the white spaces should be preserved (LoadOptions.PreserveWhitespace);
  • if the base URI should be requested from the XmlReader and, if available, should be set on the BaseUri property on all the elements loaded (LoadOptions.SetBaseUri) – as an example, when you load a document from a file, the URI points to that file;
  • if the line number should be available through the LineNumber property exposed by the XObject, which, as you recall, is the base of all the elements defined on the LINQ To XML API (do notice that you can only access that property through the IXmlLineInfo interface, that is implemented explicitly by the XObject class).

Another operation which you”ll normally end up doing is parsing a XML string. You have two options here: the first is to create a StringReader instance and pass it to initialize an XmlReader object. Then you”d just need to call one of the overloads of the Load method that receives a XmlReader. But there”s an easy way! You can simply use one of the two overloads of the static Parse method introduced by the XDocument class:

public static XDocument Parse(string text);
public static XDocument Parse(string text, LoadOptions options);

When you use one of these overloads, you”ll only need to pass it the string with the XML and that”s it. Before ending this post, there”s one thing you should keep in mind: the Load and Parse methods don”t handle exceptions thrown by the XmlReader. It”s up to you to handle it.

And that”s all for today. But there are still lots of cool stuff on this API and I”ll keep writing about it. Keep tuned!

Advertisements

~ by Luis Abreu on May 1, 2008.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: