Skip Ribbon Commands
Skip to main content

Joseph K. Perez, Sr.'s BLOG


Technology related
April 22
My New Web Site

ASP.NET 4.5 Web Application with
- SQL Server backend integration
- Social Media integration
- Membership and Subscription
- Language Localization
- Class handler for ZIP downloads
- And more.

January 25
XML, C# Class, XSD, XmlSerializer, Online documentation and aaargh
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...
  1. If you don't have an XSD file for your xml, do the following:
    • 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.
  2. Open your Developer Command Prompt for Visual Studio (your version)
  3. Change directory to the location where your xml and xsd files are located
  4. Run this command, xsd.exe yourxmlfiles.xsd /classes /f /language:CS
  5. Open your Visual Studio project and add the CS file created by xsd.exe
  6. Add "using" for the following: System.IO, System.Xml, and System.Xml.Serialization
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
        <Data1>First value</Data1>
        ...etc, etc, etc.
    <Node />
        <Data1>Second value</Data1>
        ...etc, etc, etc.
    <Node />
Here's the code you can build off of. My brain dump comments are in green text...
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Serialization;

namespace yournamespace
    class Program
        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
            dataroot dRoot;
            // 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...

XmlSerializer Class
XML Schema Definition Tool
XMLSerializer.Deserialize Class

1 - 2Next
About Me
I am currently a Developer and Build Automation Engineer based in Northern California. I have had a career in Information Technology since 1989. Previous positions I've held with large corporations in the San Francisco Bay Area helped spur my professional and technical development across a variety of platforms, operating systems, programming languages, voice and data network topologies and communications protocols. My work history demonstrates having gone full circle in the myriad of IT departments serving in support, lead, management and consulting roles. Throughout the span of my career, I've long maintained a keen interest in extending software applications to support a high level of end-user productivity.

Contact Info