I mentioned below about XML and C#….

Its taken me a freakin’ age to get my head around importing a *SIMPLE* XML file and taking the node values into code variables.  The usual pretty good VS2005 and .NET documentation was on this occasion not helpful, nor was Google..  However I finally managed it - sarcastic hurrah - in the end it took a lot of stepping through the XmlNodeReader.Read() method with this simple XML document to get even the slightest idea of how to go about this..

I’ll chuck the code I used in below, it’s probably not the cleanest way of getting the job done and the app certainly has no knowledge of the tags it has taken in.  TBH, it’s basically one step past Console.ReadLine() or StreamReader.ReadLine(), but to hell with it - it does the job I wanted it to do :D

What does it do?

I was inspired to this by this post at a blog called Soul Kerfuffle, which I stumbled across after reading the WoW post I linked to yesterday.
He’s basically using some sweet random number generating goodness to predict the outcome of NFL games.
His app is a lot sweeter than mine, taking command line switches to specify which weeks schedule he is ‘predicting’.
I suddenly had a flash of inspiration…

XML:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<root>
  <week1>
    <home>Dolphins</home>
    <away>Steelers</away>
    <home>Bears</home>
    <away>Bills</away>
    <home>Texans</home>
    <away>49ers</away>
    <home>Jets</home>
    <away>Cowboys</away>
  </week1>
</root>

So, as you can see nothing complex there, the root node contains a week1 node.  Within the week1 node, the teams are listed one after the other. (Note, this isn’t a real weeks schedule - I made this up on the train this morning).
Maybe the teams should be grouped into a game node or something rather than just dumped as is?

C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace XMLImport
{
    class Program
    {
        static void Main(string[] args)
        {           
            XmlDocument xml_doc = new XmlDocument();
            xml_doc.Load(”Schedule.xml”);
            string sz_xpath = “root”;
            string sz_Home = “”;
            string sz_Away = “”;
            XmlNode theNode = xml_doc.SelectSingleNode(sz_xpath);
            if (theNode != null)
            {
                XmlNode requestNode = theNode.ParentNode;
                XmlNodeReader nodeReader = new XmlNodeReader(requestNode);
                while (nodeReader.Read())
                {
                    if (nodeReader.NodeType == XmlNodeType.Text)
                    {
                        // the first Text node will be the home team
                        sz_Home = nodeReader.Value;
                       
   nodeReader.Read(); // move past the current nodetype.text
                       
   // cycle until we find the next Text type as this will be the away..
                       
   while (nodeReader.NodeType != XmlNodeType.Text)
                        {
                            nodeReader.Read();
                        }

                        sz_Away = nodeReader.Value;
                        Console.WriteLine(sz_Away + ” @ ” + sz_Home);
                    } // End if XMLNodeType.Text
                }// End while not EOF
                Console.Read();
            }
        }
    }
}

I’m sure you can see how it works?  I basically iterate through each line of the xml file until the current node has an XmlNodeType of Text.  When it finds the first one, I know it is going to be the home team (I wrote the XML after all :)) so I can assign the string variables then format the output to the console correctly with the form “sz_Away @ sz_Home”.

Here is the output screen, clicky for bigger…

XMLImport_Output_Screen

Next step:  Work out how to traverse an XML document looking for specific tags and then taking their content rather than assuming it’s going to be in the order I expect - or is that what defining an XML Schema is all about?  Research required :)

Any tips graciously recieved!