Tuesday, May 6, 2014

The Easiest Way to Parse XML

I was shocked to learn last week that an entire application was written parsing XML using XDocument instead of XSD.exe.  In my opinion, XSD.exe makes working with XML super simple, yes even easier than LINQ with XML.  What is XSD.exe?  I am glad you asked.

XSD.exe is a tool from Microsoft that is part of the Windows SDK.  It allows you to create an XSD from an XML file and then with great magic and fluffy bunny feet, create C# or VB.NET classes from an XSD file.  What this means for you as a developer is that you can load an XML file into a set of classes with a few lines of code.  You might also want to try xsd2code which allows you to generate generic lists for elements instead of arrays.  If you put the XSD and the generated code in your Visual Studio Solution, Visual Studio will show a nice diagram when you click on the XSD. 

Example Usage of XSD.exe

Generate XSD from an XML file:

xsd.exe myXMLFile.xml /out:c:\somepath\myXSDFile.xsd

Generate C# classes from an XSD file:

xsd.exe myXSDFile.xsd /c /out:c:\somepath

XDocument Parsing

XContainer container = XDocument.Load("c:\\myxmlfile.xml");
List<Product> products = new List<Product>();

foreach (var element in container.Descendants("products"))
    Product product = new Product();
    product.Id = element.Element("ProductId");

Parsing XML with LINQ

XDocument doc = XDocument.Load("myxmlfile.xml");
var products = doc.Root
                  .Select(x => new Product {
                              ProductId = (int) x.Element("ProductId")

Deserializing with Generated Classes

using (FileStream xmlStream = new FileStream("c:\\myxmlfile.xml", FileMode.Open))
    dataFile deserializedDataFile;

    using (XmlReader xmlReader = XmlReader.Create(xmlStream))
        XmlSerializer serializer = new XmlSerializer(typeof(dataFile));
        deserializedDataFile = serializer.Deserialize(xmlReader) as dataFile;

Validating XML in .NET and more

Sometimes XML provided by outside vendors isn't all sunshine and rainbows.  Here are some easy ways you can validate XML.

1.  In a test:

public void Ensure_XML_File_Is_Valid_XML()
    var xmlData = XDocument.Load("c:\\myxmlfile.xml);

2.  Open the XML file in Firefox.  The nice thing about Firefox compared to IE, Chrome, or Safari, is that it will actually point out where the problem is.
3.  www.freeformatter.com is a site that I use on every project.  It has an XML and JSON validators, formatters and much more. You can validate an XML file against an XSD.  Bookmark it now.
3.  XML Validator Buddy is a nice tool for seeing how many errors there are.  All the other validators simply give up after the first error.

Pretty Printing XML in C#


More Information About Parsing XML in C#