C# XML XSD Objects

Quite often our app needs to import/export XML.

Of course you can use Microsoft’s LINQ to XML to query/manipulate XML, essentially a object model, lighter weight than DOM.

However, we may prefer to work with typed an entity object and we can

  • read an XML and deserialize into an object. 
  • serialize our entity object to xml

There are many ways to achieve typed XML programming.  I will list of a couple of free approaches:

  1. xsd.exe.  This has been around for a long time and it was built by Microsoft.  It uses array.  It is .NET 4.0 compatible.  It is part of Microsoft Windows SDKs.  For example, C:\Program Files\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools.
  2. Xsd2Code is a Codeplex project.  Version 3.6 supports Visual Studio 2012 Add-in support.  It can use generic collections (i.e. List<ABC>)
  3. LinqToXsd is another Codeplex project.  This creates .NET classes with better fidelity than xsd.exe. 

The tools above require an XML Schema (*.xsd) file as an input in order to generate .NET classes.  Ways to generate XML Schema include:

  • use Visual Studio XML Editor’s menu “XML — > Create Schema”
  • use xsd.exe

Note that they only infer (i.e. guess) the XML Schema based on an existing XML file.  So choose a bigger/more complex XML file if you can.  It would be best if you can get a XML schema file from the maker of the XML file.

For me, I like LinqToXsd and Xsd2Code.  I only use Xsd.exe as a fall backup.



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