I'd like to load saved xml into specific lists. At the moment, it simply loads the 2 SceneObjects. I'm not 100% sure of the xml file being structured correctly or the code for that matter at this point. If someone would be kind enough to point me in the right direction, I'd appreciate it. I've looked into possibly combining the 3 classes into 1 to create the xml and a few other possibilities (linq) that are a bit beyond my coding level at this point. I'm writing projects to learn what I can. Thank you! XML and code below.
Code:
private void btnOpenFile_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
openFileDialog1.Filter = "XML Files (*.xml)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
//create the XmlReaderSettings object
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
//create the xml reader object
XmlReader xmlIn = XmlReader.Create(myStream, settings);
//read past all nodes until the first SceneObject node
if (xmlIn.ReadToDescendant("SceneObject"))
{
//create one waypoint object for each node
do
{
SceneObject sceneObject = new SceneObject();
xmlIn.ReadStartElement("SceneObject");
sceneObject.RunMethod = xmlIn.ReadElementContentAsString();
sceneObject.Name = xmlIn.ReadElementContentAsString();
sceneObject.Paint = xmlIn.ReadElementContentAsString();
sceneObject.Latitude = xmlIn.ReadElementContentAsDouble();
sceneObject.Longitude = xmlIn.ReadElementContentAsDouble();
sceneObject.Altitude = xmlIn.ReadElementContentAsInt();
sceneObject.Pitch = xmlIn.ReadElementContentAsDouble();
sceneObject.Bank = xmlIn.ReadElementContentAsDouble();
sceneObject.Heading = xmlIn.ReadElementContentAsDouble();
sceneObject.OnGround = Convert.ToByte(xmlIn.ReadElementContentAsString());
sceneObject.Airspeed = Convert.ToUInt32(xmlIn.ReadElementContentAsString());
sceneObjectList.Add(sceneObject);
}
while (xmlIn.ReadToNextSibling("SceneObject"));
}
//read past all nodes until the first Waypoint node
if (xmlIn.ReadToDescendant("Waypoint"))
{
//create one waypoint object for each node
do
{
Waypoint waypoint = new Waypoint();
xmlIn.ReadStartElement("Waypoint");
waypoint.Id = xmlIn.ReadElementContentAsInt();
waypoint.Flags = Convert.ToUInt32(xmlIn.ReadElementContentAsString());
waypoint.Latitude = xmlIn.ReadElementContentAsDouble();
waypoint.Longitude = xmlIn.ReadElementContentAsDouble();
waypoint.Altitude = xmlIn.ReadElementContentAsDouble();
waypoint.Speed = xmlIn.ReadElementContentAsDouble();
waypoint.Count = xmlIn.ReadElementContentAsInt();
wayPointList.Add(waypoint);
}
while (xmlIn.ReadToNextSibling("Waypoint"));
}
//read past all nodes until the first FlightPlan node
if (xmlIn.ReadToDescendant("FlightPlan"))
{
//create one flightplan object for each node
do
{
FlightPlan flightPlan = new FlightPlan();
xmlIn.ReadStartElement("FlightPlan");
flightPlan.Name = xmlIn.ReadElementContentAsString();
flightPlan.Paint = xmlIn.ReadElementContentAsString();
flightPlan.Flight = xmlIn.ReadElementContentAsString();
flightPlanList.Add(flightPlan);
}
while (xmlIn.ReadToNextSibling("FlightPlan"));
}
xmlIn.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message, "FlighT");
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<Scene>
<SceneObjects>
<SceneObject>
<RunMethod>AICreateSimulatedObjectVehicle</RunMethod>
<Name>Veh_Air_BagTractor_Euro_White_sm</Name>
<Paint />
<Latitude>55.3579907547104</Latitude>
<Longitude>-131.714398095813</Longitude>
<Altitude>92</Altitude>
<Pitch>0</Pitch>
<Bank>0</Bank>
<Heading>111</Heading>
<Onground>1</Onground>
<Airspeed>0</Airspeed>
</SceneObject>
<SceneObject>
<RunMethod>AICreateSimulatedObjectVehicle</RunMethod>
<Name>VEH_Air_BagLoaderGrey</Name>
<Paint />
<Latitude>55.3579907547104</Latitude>
<Longitude>-131.714398095813</Longitude>
<Altitude>92</Altitude>
<Pitch>0</Pitch>
<Bank>0</Bank>
<Heading>111</Heading>
<Onground>1</Onground>
<Airspeed>0</Airspeed>
</SceneObject>
</SceneObjects>
<Waypoints>
<Waypoint>
<Id>1</Id>
<Flags>4</Flags>
<Latitude>55.3579907547104</Latitude>
<Longitude>-131.714398095813</Longitude>
<Altitude>92</Altitude>
<Speed>12</Speed>
<Count>0</Count>
</Waypoint>
<Waypoint>
<Id>1</Id>
<Flags>4</Flags>
<Latitude>55.3579907547104</Latitude>
<Longitude>-131.714398095813</Longitude>
<Altitude>92</Altitude>
<Speed>12</Speed>
<Count>1</Count>
</Waypoint>
</Waypoints>
<FlightPlans>
<FlightPlan>
<Name>Beech_King_Air_350</Name>
<Paint>Beech King Air 350 Paint1</Paint>
<Flight>IFR Ketchikan Intl to Annette Island</Flight>
</FlightPlan>
</FlightPlans>
</Scene>