Monday, February 21, 2011

Working with XML in C# .NET 3.5 Using XElement, xmlTrees and LINQ to XML

The XElement class represents an XML element. All elements in XML have names, they may or may not have attributes or content.

The namespace System.Xml.Linq is required when working with XElement.

The following link provides information on the different types of content an XElement can contain.

For more information on the XElement class from the Microsoft site see the following link.

The following link provides details on members the XElement type exposes.

To create a single XML element with no content:

XElement objXElement = new XElement("Element1");

objXElement now contains the following XML:

<Element1 />

To add content to the element:

XElement obXjElement = new XElement("Element1", "ABCD");

objXElement now contains the following XML:


To create a parent element with a descendant, add a new XElement object as the content argument:

XElement objXElement = new XElement("Parent", new XElement( "ChildElement1","ABCD");

objXElement now contains the following XML:




The XML produced matches the standard XML format.

As more XElements objects are added as content arguments an xmlTree can be created.

In addition to the XElement class, there is also a XDocument class which can be used to create XML documents. The following link provides information on this class from the Microsoft site.

Working with XElement is far simpler than working with XDocument objects.
The following code snippet creates an xmlTree.

XElement xmlTreeExample =

   new XElement(“Parents”,

           new XElement("Parent",

                   new XElement( "ChildElement1","ABCD"),

                   new XElement(“ChildElement2”,

                           new XElement(“GrandChildElement1”, “1234”)


xmlTreeExample now contains the following XML:









Creating an xmlTree using XElement.Parse

The above example is not the only way to create an xmlTree, XElement’s Parse method can be used to create an xmlTree from a string containing XML.

XElement xmlTreeExample2 =

   new XElement.Parse(xmlString, LoadOptions.None);

LoadOptions set to None will not preserve white spaces or retain line information. If this is required then set LoadOptions to LoadOptions.PreserveWhitespace or LoadOptions.SetLineInfo. Please note, using LoadOptions.SetLineInfo will mean taking a performance hit.

Creating an xmlTree using XElement.Load

Using the code snippet above, the XElement object ‘xmlTreeExample2’ can be saved in an XML file.


The Load method allows an XElement to be loaded from an XML file.

XElement xmlTreeExample3 = XElement.Load(“TestTree.xml”);

The Load method can also be used to load an XElement from a TextReader, xmlReader and using a stream an XElement instance can be created. See the following link for more information on these alternative methods.

The following link provides more information on creating xmlTrees
LINQ to XML is an approach to programming XML in .NET using LINQ query expressions. Query results can be used as parameters in XElement and XAttribute objects when creating xmlTrees. This functionality(known as functional construction) allows xmlTrees to be transformed from one shape to another.
Simple LINQ to XML queries
XElement xmlTreeExample4 contains the following XML:













    </ Parent>


The following code snippet shows how to retrieve the value of the element ChildElement1 from each parent element in xmlTreeExample4.

IEnumerable elementValue =

             from parent in xmlTreeExample4.Descendants(“Parent”)

             select (string) parent.Element(“ChildElement1”);
The following code snippet shows how to retrieve the value of the element GrandChildElement1 from each parent element in the xmlTreeExample4.

IEnumerable elementValue =

             from parent in xmlTreeExample4.Descendants(“Parent”)

             select (int) parent.Element(“ChildElement2”).Element(“GrandChildElement1”);
To retrieve the values of all descendants of each Parent element the following code shows how this can be achieved.

var parentData = from parent in xmlTreeExample4.Descendants(“Parent”)

select new


            child = parent.Element(“ChildElement1”).Value,

            grandchild = parent.Element(“ChildElement2”).Element(“GrandChildElement1”).Value


A for each loop can be used to loop through each object in parentData, like so

foreach (var parent in parentData)

             //do some processing here

If each parent element in xmlTreeExample4 contained an attribute called ‘ParentName’, for example

<Parent ParentName=”FirstParent”>

To retrieve this ‘ParentName’ attribute value, the following can be done

IEnumerable attributeValue =

            from parent in xmlTreeExample4.Descendants(“Parent”)

            select (string) parent.Attribute("ParentName");
See the Microsoft site for more information on LINQ to XML

For more information on Programming C# LINQ Query Expressions see this link

Thursday, February 3, 2011

Developing Rest Web Services using .NET WCF

There are a number of ways to create a rest web service in visual studio.

1)Convert an existing WCF soap web service into a WCF rest web service.

The following 15 minute video shows how easy it is to accomplish this.

2)Download Microsoft's WCF Rest Starter Kit from the following codeplex site:

A white paper on developing rest in WCF using the rest starter kit can be found here:

The starter kit contains various templates to speed up the process of creating services. The templates include an Atom Feed WCF Service, AtomPub WCF Service, HTTP Plain XML WCF Service, REST Collection WCF Service and a REST Singleton WCF Service.

3)Create a WCF Rest web service from scratch using a visual studio solution containing for example the following projects:-

ASP.NET Web Application project template - this project is used to create a rest service. It contains a service contract class with operation contracts and data contracts.

A Web Service template is also added to this project, this template is a visually designed class for creating a web service. This is where the implementation code for the operation contracts of the web service will be placed.

Windows Forms Application project template - used to host the rest web service. This project contains an app.config file and a code file with service host code.