Staple it together
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
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…
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!
You can also subscribe via FeedBurner and receive my latest posts by email. If you prefer to use an RSS reader, you can subscribe using this link.
Uhm…. Dude, you’re going ’round the horn and taking the long way. First, structure your xml a little better.
DolphinsSteelers
……
Try something like:
foreach( XmlNode node in xml_doc.SelectNodes( “/root/week” ) ){
string weeknumber = node.Attributes[ "name" ].Value;
foreach( XmlNode gamenode in node.SelectNodes( “game” ) ){
string home = gamenode.SelectSingleNode( “home” ).InnerText;
string visit = gamenode.SelectSingleNode( “visitor” ).InnerText
string line = string.Format(
“Week {0} games: {1} at {2}”,
weeknumber, visit, home );
Console.WriteLine( line );
}
}
This way your xml and xpathing won’t have to have that gawdawful Sax style document crawling, you can use XPath much more efficiently, and your code only has to know the basic structure of the data without having to know which week number you’re selecting as part of the node name. You can XPath that as well… something like “/root/week[name='2']” as your xpath in the first loop to select only week2’s games.
HTH
What’s up! Glad to see you at least got things up and running. If you want I’ll send you the code for how I did it… basically I just make some simple HTTP calls to random.org and then spin through the XML doc.
Take it easy!
Hey,
Thanks for the pointer, Chornbe. It looks like the xml you added to the comment was stripped out but I think I’ve got what you meant from your code example
which by the way was very helpful, thanks!
RYS - cheers, I wouldn’t mind taking a look if you don’t mind?