Saturday, December 05, 2009

How to create a C# Class from XML file (via xsd.exe)

A friend asked me a couple of times about how to I created the class I used in “twitter common friends” to handle xml as objects (includes reading and writing back). Actually the tool (xsd.exe) is pretty simple and takes no more than a couple of commands to create the class for me.

It’s just that I usually forget the keywords/parameters & their order (you can create a dataset or class that can be C# or VB, and there’s a couple of other option I never used before). I usually refer to its MSDN page when asked..

So I thought a post would be easier to remember. First you should create the xsd file out of the xml, using the VS command prompt:
xsd myFile.xml
That will create “myFile.xsd”, now to create “myFile.cs”, write the below line:
xsd myFile.xsd /c 
Now you can add the cs file to your project and use it as illustrated in my previous post.


Cross-posted from FCIH-Blog

2 comments:

simi said...

hi,

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I

was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful

for .Net community.

There is a good C# resource site, Have alook

http://www.csharptalk.com/2009/09/c-class.html
http://www.csharptalk.com

simi

Shady M. Najib said...

Are you "Simi" or "Rahul"? Seems like you're a bot my dear :D

Disclaimer

All the opinions expressed on this blog are my own and don't necessarily represent my employer's positions, strategies or opinions.