There will be a time in your life that independently of your age, IQ, experience with codes or your geek level, you will need to face a XML, and better yet, you will need to face a XSD which describes the entire XML.
So you think: What now? Should I just forget it? Turn on my Xbox 360 and play ’till death? The answer is: NO, just CALM down and let .NET and Microsoft SDKs make the hard job for you and see how .NET can show his power.
Let’s make the entire problem clear. First you have a valid XSD schema and you need to generate a valid XML following the same schema with some information’s that you have in your database or whatever you need.
Your XSD file looks something like that:
Still thinking: what XSD SDK can make for me to make me smile again? XSD SDK transforms your entire XSD file in a C# class making easier to work and understand the context.
Hand’s on, let’s code.
With the XSD file ready, you’ll need to find the XSD application in your Microsoft SDKs folder and paste the XSD file there.
Usually you can find XSD Application on C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin.
Open your CMD (Console, terminal) and find the location of the XSD SDK.
Now if you write “xsd” and press enter, you will see lots of options that are a simple help to teach you how to use XSD SDK to generate your classes of XMLs.
Let’s see the .NET magic. Type this next line in your console:
xsd.exe -c -l:c# -n:PlayingAround_XSD_XML file.xsd
A short explanation about the line:
- “Xsd.exe” you call the SDK and pass the parameters;
- “C#” indicates which language you are using;
- “-n:PlayingAround_XSD_XML” shows which namespace we want in our class that will be generated by the SDK;
- “file.xsd” indicates the name of the XSD file.
Yes, our XML C# class was generated with success!
See? .NET makes your programmer’s life easier and much more productive.
Let’s open our file.cs and see what we get so far:
And much more codes…
So you ask me: “That’s the code? That seems pretty confusing…”
Don’t worry, you don’t need to care about that code, .NET generates it to make our lives easier, you don’t need to know how that code works, with a little basic explanation things can be just fine.
.NET split the entire XSD schema in partial classes that represents all nodes of the XML so we can use it like entities on our code. You don’t need to write node by node, you just make entities and put some values into.
Let’s try using that class, now opening your Visual Studio and create a new solution.
Put the file.css that was generated by the SDK into your project.
Open your Form1.cs and double click in the screen, will create a “load event” for the form.
See? I just create the root node and the Visual Studio starts to guide me, showing the information that I need to put there to make that XML valid. If your try another nodes of the root, you will see common types like strings and integers. Let’s write a little bit of code.
Now we write all we need in our XML. Easy, isn’t?
Just putting some information into entities we’ll have our XML complete. What’s the next step?
Export our file.xml and then our job will be complete.
Press F5 and run the application, then go to the debug folder, you should see our file there:
Open the FirstXML.xml file and we are done. Here’s our first XML generated with .NET:![]()
So that’s it, our first XML was generated perfectly and easily using .NET and XSD SDKs. The point is, there is always another and easier way to implement a solution and .NET can help you in most of the cases.
Especial thanks to Joberto Diniz who starts the idea of generating the XML with XSD SDK and Talita Facirolli for text corrections.
The entire code and this tutorial (including Portuguese version) are available on @stJhimy’s github for download.
Feel free to make comments and criticisms.
Cya and enjoy the tutorial.


[...] This post was mentioned on Twitter by Joberto Diniz Junior, Jhimy F. Villar. Jhimy F. Villar said: http://bit.ly/HMsgX, A Basic tutorial about xsd and xml using c# [...]
Hi, great tutorial, keep the good work
[...] Syndicated from AWP [...]
I am having problems to generate classes for these XSD
http://www.nfe.fazenda.gov.br/PORTAL/docs/PL_005d.zip
Could please try to generate C# classes from it, and post how to do it?
Send me an email explaining what you want , i will help you : )
stjhimy@gmail.com