LINQ to XML and .NET 3.5 C# Help

With the introduction of LINQ to the .NET Framework 3.5, the focus was on easy access to the data that you want to work with in your applications. One of the main data stores in the application space is XML and, therefore, it really was considered a no-brainer to create the LINQ to XML implementation, Prior to the LlNQ to XMLrelease, working with XML using System.Xml was really not the easiest thing in the world to achieve, With the inclusion of System, Xml. Linq, you now find a series of capabilities that make the process of working with XML in your code that much easier.

New Objects for Creating XML Documents

In creating XML within application code, many developers turned to the XmlDocument object to do this job, This object allows you to create XMLdocuments that enable you to append elements, attributes, and other items in a hierarchical fashion. With LINQ to XMLand the inclusion of the new System. Xml. LinQ namespace, you will now find some new objects that make the creation of XMLdocuments a much simpler process.

Visual Basic 2008 Ventures Down Another Path

An interesting side note to the LINQ to “ML feature set is that the Visual Basic 2008 team at Microsoft actually took the LINQ to XML capabilities a little further in some areas: For instance, something you are unable to accomplish in C# 2008 that you can do in Visual Basic 2008 is include XML as a core part of the language. XMLliterals are now a true part of the Visual Basic language and you are able to paste XML fragments directly in your code for inclusion, and the XML included is not treated as a string.

Namespaces and Prefixes

One issue that was somewhat ignored in parts of the .NET Framework 2.0 was how the items in the framework dealt with the inclusion of XMLnamespaces and prefixes in documents. LINQ to XMLmakes this an important part of the XMLstory, and you will find the capabilities to work with these types of objects to be quite simple.

New XML Objects from the
.NET Framework 3.5

Even if the LlNQ querying ability wasn’t available in this release of the .NET Framework, the new XML objects provided by the .NET Framework 3.5 to work with the XML that are available in place of working directly with the COM in this release are so good, that they even can stand on their own outside of LlNQ. Within the new Sys tem. XDll.Little namespace you will find a series of new LlNQ to XML helper objects that make working with an XML document in memory that much easier.

The following sections work through the new objects that are available to you within this new namespace Many of the examples in this chapter use a file Called Hamlet.

xml, This is a file you can find at zip that includes all ofShakespeJzre’s plays as XML files.


The XDocument is a replacement of the XML Document object from the pre-.NET 3.5 world. You will find the XDocument object easier to work with in dealing with XML documents. The XDocument object works with the other new objects in this space, such as the XNamespace, XCOllmlent,XElement, and XAttribute objects.

One of the more important members of the XDocument object is the Load () method:
XDocument xdoc = XDocument.Load(i’C:\Hamlet.xml’);
This operation will load up the Hamlet. XIIIlcontents as an in-memory XDocument object. You are also
able to pass a TextReader or XDllReader object into the Load () method. From here, you are able to
programmatically work with the XML:
XDocument xdoc = XDocument.Load(i’C:\Hamlet.xml’);
This produces the following results:.
Another important member to be aware of is the Save () method, which, similar to the Load () method, allows you to save to a physical disk location or to a TextWriter or XmlWriter object:
XDocument xdoc = XDocument.Load(i’C:\Hamlet.xml’);


One of the more common objects that you will work with is the XElement object. With these objects, you are easily able to create just single-element objects that are XML documents themselves and even just fragments of XML. For instance, here is an example of writing an XML element with a corresponding value:
XElement xe = new XElement(‘Company’, ‘Lipper’);

In the creation of a new XElement object, you are able to define the name of theelement as well as the value used in the element. In this case, the name of the element will be ~company>, and the value of the <Company> element will be Lipper. Running this in a console application with a System.XmI :Linq reference produces the following result:

You, are able to create an even more complete XMLdocument using multiple XElement objects, as illustrated in the following example:

Running this application produces, the results illustrated in Figure 29-2.

Figure 29-2

Figure 29-2


The XNamespace is an object that represents an XML namespace and is easily applied to elements within your document. For example, you can take the previous example and easily apply a namespace to the root element:

In this case an XNamespace objectis created by assigning it a value of
ns I 1.From there,itisactuallyused in the root element <Company> with the installation of the XElement object:
XElement xe = new XElement(ns + ‘Company’, II …
This produces the resultsillustratedin Figure 29-3.

Figure 29-3

Figure 29-3

In addition to dealing with only the root element, you can also apply namespaces to all your elements as shown in the following example:

This produces the results shown in Figure 29-4.

Figure 29-4

Figure 29-4

In this case, you can see that the sub-namespace was applied to everything you specified except for the <Address>, <City>, <State>, and the <country> elements because they inherit from their parent, <CompanyAddress>, which has the namespace declaration.


The XComment object allows you to easily add XML comments to your XMLdocuments, The following example shows adding a comment to the top of the document:

Here, an XDocument object that contains two XMLcomments is written t? the console, one at the top of the document and another within the <CompanyAddress> element. The output of this is presented in Figure 29-5.

Figure 29-5

Figure 29-5


In addition to elements, another important factor of XML is attributes. Adding and working with attributes is done through the use of the XAttribute object, The following example shows adding an attribute to the root <Customers> node:

Here, the attribute My Attribute with a value of My AttributeValue is added to the root element of the XML document, producing the results shown in Figure 29-6.

Figure 29-6

Figure 29-6

Posted on October 31, 2015 in LINQ to XML

Share the Story

Back to Top