Skip Ribbon Commands
Skip to main content

Joseph K. Perez, Sr.'s BLOG

:

Home
Technology related
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
 
<dataroot>
    <Node>
        <Data1>First value</Data1>
        ...etc, etc, etc.
    <Node />
    <Node>
        <Data1>Second value</Data1>
        ...etc, etc, etc.
    <Node />
</dataroot>
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')
                Console.WriteLine(oNode.Data1);
            }
        }
    }
}
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
https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.110%29.aspx
 
XML Schema Definition Tool
https://msdn.microsoft.com/en-us/library/x6c1kb0s%28v=vs.110%29.aspx
 
XMLSerializer.Deserialize Class
https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.deserialize%28v=vs.110%29.aspx
 

May 13
Client / Server Overview

Example of Client / Server software

Microsoft OutLook email (client) communicating with a remote Microsoft Exchange (Server) using standard port numbers for SMTP and POP3 Client processes don't use well-known or registered ports. Instead, each client process is assigned a temporary port number for its use. This is commonly called an ephemeral port number.

ephemeral: “short-lived; existing or continuing for a short time only.”

Client processes contact server processes using the server's host IP address and registered port number. To know where to send the reply, the server must know the port number the client is using. This is supplied by the client as the Source Port in the request, and then used by the server as the destination port to send the reply. The graphic below, Sample IP Packet, shows the structure of a request.
 
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