Say, it's the year 2015, and you'd be surprised that there's still so much unnecessary (two n's and two s's. Thanks Google!) jargon and other noise words on the Internet when all you're looking for is the "straight ahead" stuff. So, here's the deal. I'm going to skip some rants about documentation in general, and just dive into some serious straight ahead coding jazz demonstration.
Follow this situation...
- You have a C# Console Project
- You have an XML file
- You want to use a Class file to represent your XML file
Here's the sequence of steps...
- If you don't have an XSD file for your xml, do the following:
Open your Developer Command Prompt for Visual Studio (your version)
Change directory to the location where your xml and xsd files are located
Run this command, xsd.exe yourxmlfiles.xsd /classes /f /language:CS
Open your Visual Studio project and add the CS file created by xsd.exe
Add "using" for the following: System.IO, System.Xml, and System.Xml.Serialization
- Import your xml file into Microsoft Excel
- Save it as a tab delimited file
- Create a database using Microsoft Access, import your tab delimited file as a linked table
- Export your linked table to xml and be sure to check the box to generate the XSD schema for your xml file
- I'm giving you the above steps because chances are, you're probably going to have a hard time finding the related tools most online articles state you should have with Visual Studio.
Now, comes the part where I really get all "straight ahead jazzy" on you, and save you lots of time reading online articles trying to learn how to do the most simple basic stuff such as as read an xml file, put the data into an object format and get data. Try to find something as point blank as this on the Internet doing a Google search, and if you do, you're one lucky jazz cat. Well, anyhow, forget the fear and loathing you got from examining the generated class file from xsd.exe, and forget what the online naysayers are saying online in the forums. This is as easy as pie. Well, it wasn't at first. When I first started doing this, I pretty much had to do a, wrack-the-brain and translate-goofy-internet-articles dance and song before I discovered how easy it was. I got sore feet from the ordeal ;)
This example uses an xml file with a structure like this as it was exported by Excel, consumed and exported by Access:
Contents of your.xml file
...etc, etc, etc.
...etc, etc, etc.
Here's the code you can build off of. My brain dump comments are in green text...
static void Main(string args)
// dataroot is available in intellisense after you've imported
// the class file generated by XSD
XmlSerializer xSer = new XmlSerializer(typeof(dataroot));
// now open your xml file and put it into a file stream object
FileStream fs = new FileStream("your.xml", FileMode.Open);
// instantiate an empty object that represents an instance of your class
// now we take the serialized xSer object that represents your xml file
// and "transfer" it into your dRoot object which contains the entire
// structure of your xml file, including the data
dRoot = (dataroot)xSer.Deserialize(fs);
// now, we want to put all of the data into an iterable array
// where we can reference each node individual
Node oNodes = dRoot.Node;
// now it's just a matter of iterating through the array
// and doing what you need to
foreach (Node oNode in oNodes)
// display the value in the Data1 node which should be 'Some value')
You just can't get anymore straight ahead than that. But, okay, if you're new at this, let's be responsible about it huh, and make sure you click on the links below just to be sure you understand what's underneath the hood. It's kind of funny as I look at this article of mine. For some reason, I imagine Microsoft MSDN as a persona of Captain Kirk from Star Trek, and my little article here as a persona of Doc Bones with an exasperated facial expression saying, "Dammit Jim! Spare me the rhetoric! Just give me the damn step by step!!"
And, now, for the example-less spiels...
XML Schema Definition Tool