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
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