Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a XML file and using certain parts 1

Status
Not open for further replies.

airprox

Programmer
Dec 2, 2006
12
GB
Hi Folks,

Back again! OK... My program has grown, now when I press a button, I would like to open a xml file, which looks something like this...

//This is an example of the XML files
<?xml version="1.0" encoding="ISO-8859-1"?>
<FSData
version="9.0"
xmlns:xsi=' xsi:noNamespaceSchemaLocation="bglcomp.xsd" >

<SceneryObject
instanceId="{be8afc79-336d-4f8f-b0b1-6f20479b2e9b}"
lat="58 11.44348
lon="000 6.78311"
alt="19.000"
pitch="0"
bank="0"
heading="168.8"
altitudeIsAgl="TRUE"
imageComplexity="VERY_SPARSE">

<LibraryObject
name="{63d62a5f-97bc-475f-83ea-e848ee97d3e6}"
scale="1.0" />

</SceneryObject>

<ModelData
sourceFile="30Mplatform.MDL" />

<SceneryObject
lat="58 11.50436"
lon="000 6.77295"
alt="37"
pitch="0"
bank="0"
heading="0"
altitudeIsAgl="TRUE"
imageComplexity="VERY_SPARSE">

<Effect
effectName="fx_ODGFlareOil" />

</SceneryObject>

</FSData>
// End of XML file

When the file is open I would like to parse (is that the correct term?) the file and in the first section of "SceneryObject" (and only that section... there will be alot of others!) get the value for "lat" and show it in textBox1, "lon" in textBox2 and "alt" in textBox3 (I've highlighted the vaues I'm after in red). I've been trying all night... and have come up with the following which doesn't work, although it does open the file... I think.

// My button click code
private void button4_Click(object sender, EventArgs e)
{
// Create an OpenFileDialog object:
OpenFileDialog openFilez = new OpenFileDialog();

// Create dialog title
openFilez.Title = "Open Pad XML File";

// Initialize the OpenFileDialog to look for text files:
openFilez.Filter = "xml Files|*.xml";

// Set initial directory
openFilez.InitialDirectory = "Pads\\";

// Check if the user selected a file from the OpenFileDialog:
if (openFilez.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{

MessageBox.Show(openFilez.FileName.ToString());

}

openFilez.CheckFileExists = true;

openFilez.CheckPathExists = true;

XmlTextReader padReader = null;
padReader = new XmlTextReader(openFilez.FileName);
padReader.Read();
padReader.Read();

while (padReader.Read())
{
if (padReader.NodeType == XmlNodeType.Element)
{
if (padReader.LocalName.Equals("lat"))
{
textBox1.Text = (padReader.Value);
}

if (padReader.LocalName.Equals("lon"))
{
textBox2.Text = (padReader.ReadString());
}

if (padReader.LocalName.Equals("alt"))
{
textBox3.Text = (padReader.ReadString());
}
}
}

padReader.Close();

}
}
}
// End of button click

If anyone is still with me this far down the post... could you please point out where I'm going wrong? I also realise theres probably a lot wrong! BTW, if you hadn't guessed, I'm a complete newbie!
Cheers
Alun
 
padReader.LocalName refers to the node, not its attributes.

You want to look at doing this:

Code:
if (padReader.LocalName.Equals("SceneryObject"))
{
textBox1.Text = padReader.GetAttribute("lat");
textBox2.Text = padReader.GetAttribute("lon");
textBox3.Text = padReader.GetAttribute("alt");
}
 
OK... I'll give that a go tonight. That does seem to make more sense.

Can I also check then that the lines,
padReader.Read();
padReader.Read();
are correct to make the reader skip the first 2 elements:
<?xml version="1.0" encoding="ISO-8859-1"?>
<FSData
version="9.0"
xmlns:xsi=' xsi:noNamespaceSchemaLocation="bglcomp.xsd" >

and that the code you've supplied will only look at that first element to contain "SceneryObject"?

Thanks for your help!
Alun
 
you dont need to skip elements - if none of the elements meet your code that says you're looking for sceneryobject, they'll be skipped.

so the code i privided says.. if the Element Node the reader is on is named "SceneryObject", grab the values of the attributes within.
 
Hi again,

Thanks for the explanation... much clearer to me now, however, just tried the code and it's not working for me. Stepping through the code, it skips through the first couple of bits (I took out the 2 lines I mentioned above and let your code do the work) then it recognises the "SceneryObject" part and steps into the GetAttribute part then carries on stepping through the other bits but at the end nothing is displayed in the textboxes.

Any clues?

Cheers
Alun
 
In addition, I changed the first textBox to a messageBox.Show (padReader.GetAttribute("lat"));
to test it was reading the right things and a messageBox opens with the figures for "lat", I click OK then the remaining 2 textBoxes fill with the correct data and a ...

Disregard... just figured it out ;-) I've added a break; after the last textBox line, so when it fills the boxes with the info from the first SceneryObject it finds it doesn't carry on looking, which is what it was doing then getting to the last SceneryObject instance which is actually empty, which in turned emptied the boxes! Cuffed to bits! <-- UK expression.

Many thanks once again for your help!
Cheers
Alun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top