Generating a C# Class from XML

By | 07/05/2013

After a two week hiatus from work and blogging, spent mostly at the ever excellent Reading Beer Festival, I’m back at work and doing something so simple I’ve utterly forgotten how to do it.  As this blog started as a way for me to keep track of random things I learn over the years, it’s time to write this one down.

Here’s the issue; I’ve created an XML file and want to create a C# class library to work with it in code.  It’s really easy once you remember how, through the use of xsd.exe.

  1. Create xml file
  2. Run “xsd.exe filename.xml” to create an XSD file
  3. Run “xsd.exe /classes /language:CS filename.xsd” to generate a .CS file

One thing that always makes me chuckle is this line that is included at the top of each CS file;
“This code was generated by a tool.”

In this case, a forgetful one at that.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.