Fun with System.Xml.Linq.XDocument
Overview
I've recently been using some new code to do some standard xml manipulation. Although I am pretty comfortable working with an XmlDocument I was interfacing with some code from another team and they were using the newer XDocument. I decided to give it a try and see how well it worked. Needless to say, it's very different from an XmlDocument. On the other hand, it provides a very intuitive interface and is very easy to use.
I've provided a few examples of adding and updating Elements (this APIs version of Nodes) and also of moving between the traditional XmlDocument and the newer XDocument. I used the XDocument's Parse method to create a new object directly from a string and was off to the races.
Here is the code for the examples:
Program.cs
namespace BlogConsoleDemoApp
{
class Program
{
static void Main()
{
XDocumentDemo.DemoXDocUsage();
}
}
}
XDocumentDemo.cs
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace BlogConsoleDemoApp
{
public class XDocumentDemo
{
public static void DemoXDocUsage()
{
XDocument xDocSource = XDocument.Parse("<?xml version=\"1.0\" encoding=\"utf-8\"?><albums><album id=\"1\"><name>Get your Ya-Yas Out</name><artist>The Rolling Stones</artist><rating>5</rating></album><album id=\"2\"><name>The White Album</name><artist>The Beatles</artist><rating>5</rating></album><album id=\"3\"><name>Baba O'Reilly</name><artist>The Who</artist><rating>5</rating></album><album id=\"4\"><name>Houses of the Holy</name><artist>Led Zepplin</artist><rating>5</rating></album><album id=\"5\"><name>All Along the Watchtower</name><artist>Jimi Hendrix</artist><rating>5</rating></album></albums>");
XDocument xDoc = XDocument.Parse("<?xml version=\"1.0\" encoding=\"utf-8\"?><myalbums><album id=\"121\"><name>Toys in the Attic</name><artist>Aerosmith</artist><rating>4</rating></album><album id=\"34\"><name>Alive</name><artist>KISS</artist><rating>3</rating></album><album id=\"23\"><name>Goodbye Yellow Brick Road</name><artist>Elton John</artist><rating>4</rating></album><album id=\"229\"><name>Girlfriend</name><artist>Matthew Sweet</artist><rating>3</rating></album><album id=\"244\"><name>Live at the Sands</name><artist>Frank Sinatra</artist><rating>5</rating></album></myalbums>");
Trace.WriteLine("Original Xml");
Trace.WriteLine(xDoc.ToString());
// create a new rating element and replace the existing element (retrieved using XPath) with the new element
XElement oldXElement = xDoc.XPathSelectElement("myalbums/album[@id = '34']/rating");
XElement newXElement = new XElement("rating", "5");
if (oldXElement != null) oldXElement.ReplaceWith(newXElement);
Trace.WriteLine("");
Trace.WriteLine("******************************************************************************");
Trace.WriteLine("Rating changed on the KISS album");
Trace.WriteLine(xDoc.ToString());
// Get the Matthew Sweet entry using XPath and replace it with Led Zepplin
XElement oldXElementToBeReplaced = xDoc.XPathSelectElement("myalbums/album[@id = '229']");
XElement newXElementToReplaceWith = xDocSource.XPathSelectElement("albums/album[@id = '4']");
if (oldXElementToBeReplaced != null) oldXElementToBeReplaced.ReplaceWith(newXElementToReplaceWith);
Trace.WriteLine("");
Trace.WriteLine("******************************************************************************");
Trace.WriteLine("Matthew Sweet replaced by Led Zepplin");
Trace.WriteLine(xDoc.ToString());
// create and add this new element
//<album id=\"2007\"><name></name><artist>Foo Fighters</artist><rating>4</rating></album>
XElement newXElementAdd = new XElement("album");
newXElementAdd.SetAttributeValue("id", "2007");
newXElementAdd.SetElementValue("name", "There Goes My Hero");
newXElementAdd.SetElementValue("artist", "Foo Fighters");
newXElementAdd.SetElementValue("rating", "4");
if (xDoc.Root != null) xDoc.Root.Add(newXElementAdd);
Trace.WriteLine("");
Trace.WriteLine("******************************************************************************");
Trace.WriteLine("Foo Fighters Created on the fly and added");
Trace.WriteLine(xDoc.ToString());
// Convert to XmlDocument
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xDoc.ToString());
Trace.WriteLine("");
Trace.WriteLine("******************************************************************************");
Trace.WriteLine("Convert to XmlDocument for use with existing methods");
Trace.WriteLine(xmlDoc.InnerXml);
// Convert Back to XDocument
XDocument newXDoc = XDocument.Parse(xmlDoc.InnerXml);
Trace.WriteLine("");
Trace.WriteLine("******************************************************************************");
Trace.WriteLine("Convert back to XDocument when done");
Trace.WriteLine(newXDoc.ToString());
}
}
}
I hope it helps - take care until next time.