Hello all,

Okay, I've been beating my head for a couple of days over this one and I'm running out of time. I'm writing an app that reads an XML file, then uses the data in the XML file as variable values. Here's a sample XML file:

<!--Test.xml-->
<Test>
   <turn>right</turn>
   <date>1/1/2013</date>
   <beer>ale</beer>
   <loop>5</loop>
   <work/>
   <next>alum</next>
</Test>

So, I need the application to read this XML file and the populate those element values as string variables in the application. USing XmlDocument, I load it into my application:

string loadx = (Globalvar.Drv + "\\bin\\Mig\\xml\\test.xml");

XmlDocument xpmt = new XmlDocument();
xpmt.Load(loadx);

Then I loop through the XML to read the elements and store the values in the appropriate variables:

foreach (XmlNode xnode in xpmt.SelectNodes("Test"))
    {
          Globalvar.turn = (xnode.SelectSingleNode("turn").InnerText);
          Globalvar.date = Convert.ToDateTime(xnode.SelectSingleNode("date").InnerText);
          Globalvar.beer = (xnode.SelectSingleNode("beer").InnerText);
          Globalvar.loop = Convert.ToInt16(xnode.SelectSingleNode("loop").Value);
          Globalvar.work = (xnode.SelectSingleNode("work").InnerText);
          Globalvar.next = (xnode.SelectSingleNode("next").InnerText);
    }

Note: Globalvar is my global variable class.

I have the code loaded in private void Mtool_Load(object sender, EventArgs e) so that the XML is read at the start of the application.

To test that the variables are being populated from the xml data, I have test code to write those variables on a form label like this:

label8.Text = Globalvar.beer;

The output is just a blank label, AND, the date and integer variables throw and error in the label code:

label2.Text = Convert.ToDateTime(Globalvar.date);
label17.Text = Globalvar.loop;

I'm using Visual Studio 2010 and its a .NET 2.0 framework, and I do have using System.Xml; in there.

I'm new at coding, but this feels like the problem is smacking me in the face, yet I cant see it. Any help would be greatly appreciated.

Thanks,
Hendo

Re: XML to Variable values 80 80

Use Value not InnerText.

Re: XML to Variable values 80 80

Use Value on all of them?

Re: XML to Variable values 80 80

i was looking for this code....thanx for sharing...

commented: Signature spammer. +0
Re: XML to Variable values 80 80

Value didn't work. I'm still showing a blank result.

Re: XML to Variable values 80 80

Put breakpoint on the line and see what the value is.

Re: XML to Variable values 80 80

The values are empty

Re: XML to Variable values 80 80

Ok...I went to XmlTextReader to do the job, and that works. Thanks for your time, Momerath! I do appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.