0

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>
kaedwards
  • 1
  • 2

3 Answers3

0

Have you thought about using AutoMapper.

There is a good codeplex tutorial by Scott Toberman that shows you can map XML to Lists. It is a little dated but give a good overview. Another one here by Stan Bashtavenko may help.

Another path would be to use c# serialization using the XSD.exe tool to generate the required object structure and XML schema as suggested by ckarras.

Community
  • 1
  • 1
Boden
  • 221
  • 1
  • 3
  • 15
0

Depending on the purpose for creating the lists, you may find it better to use DataTables. A DataSet will load/write your xml file, storing the data in DataTables. Once in the Datables you can act on the data however you wish, edit, display, etc.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22
0

You can follow this simple step

1.Please Add using System.Xml as a reference;
2.Make a class named book in this way



     public class book
            {
                public Nullable<System.DateTime> date{ get; set; }
                public decimal price { get; set; }
                public string title { get; set; }
                public string description { get; set; }
        }

    try
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load("Write down full path");
                    XmlNodeList dataNodes = xmlDoc.SelectNodes("/catalog");

                    foreach (XmlNode node in dataNodes)
                    {
                        book objbook = new book();
                     objbook.date=Convert.ToDateTime(node.Attributes["date"].Value);
                       objbook.title=node.SelectSingleNode("title").InnerText;
                   objbook.description=node.SelectSingleNode("description").InnerText;
objbook.price=Convert.ToDecimal(node.SelectSingleNode("price").InnerText);

                    }

                }
catch(Exception ex)
{
throw ex;
}

Here I use one class,you can make another class. when Serializing,then assign the serialized data to the object of both class.

anis programmer
  • 989
  • 1
  • 10
  • 25