简体   繁体   中英

How read the element value inside a node in a XML file using C#

Good morning , I have a template file written as XML file in this way

<Simulation>
<Pedestrian Name="Mother">
    <Initial_Position In_X="3" In_Y="3" />
    <Final_Position>
        <First Fin_X="6" Fin_Y="6" Time="2" />
    </Final_Position>
</Pedestrian>

I implemented a Class in order to read the file.

while (reader.Read() || i<Number_of_pedestrian)
{
    if (reader.Name == "Pedestrian")
    {
        if (reader.HasAttributes == true)
        {
            name = reader.GetAttribute("Name");
            //MessageBox.Show(name);                            
        }
    }

    if(reader.Name == "Initial_Position")
    {
        if (reader.GetAttribute("In_X") != null && reader.GetAttribute("In_Y") != null)
        {
            X1 = int.Parse(reader.GetAttribute("In_X"));
            Y1 = int.Parse(reader.GetAttribute("In_Y"));
        }
    }

    if (reader.Name == "Initial_Position")
    {
        if (reader.GetAttribute("Fin_X") != null && reader.GetAttribute("Fin_Y") != null)
        {
            X2 = int.Parse(reader.GetAttribute("Fin_X"));
            Y2 = int.Parse(reader.GetAttribute("Fin_Y"));
        }
    }
    //Position Initial_Position = new Position (X1,Y1);
    //Position Final_Position = new Position(X2, Y2);

    Pd[i]=new Pedestrian (name, X1, Y1, X2, Y2);
    Pd[i].Draw();
    i++;
}

Which is able to read any attribute (in this case " Name ") but is no able to read inside a node and then take the attribute (in this case inside " Initial_Position " and then " In_X ").

Moreover the line Pd[i]=new Pedestrian (name, X1, Y1, X2, Y2); give me the following error :

System.IndexOutOfRangeException occurs. 
Additional Information : index over limits of matrix

Make sure that give desired XML. make sure every tag of your XML has a close tag, make sure that <Pedestrian Name="Mother"> has a closing tag. Then check X's and Y's before executing

Pd[i]=new Pedestrian (name, X1, Y1, X2, Y2);
Pd[i].Draw();

And have a look at Loading XML String into datatable .

Why don't you do this in LINQ to XML instead. It's much simpler and the code is a lot cleaner:

using System.Xml.Linq;

string xml = "<Simulation><Pedestrian Name='Mother'><Initial_Position In_X='3' In_Y='3' /><Final_Position><First Fin_X='6' Fin_Y='6' Time='2' /></Final_Position></Pedestrian></Simulation>";
XDocument doc = XDocument.Parse(xml);

foreach (XElement pedestrian in doc.Root.Elements("Pedestrian"))
{
    XElement initialPosition = pedestrian.Element("Initial_Position");

    string name = pedestrian.Attribute("Name").Value;
    string x = initialPosition.Attribute("In_X").Value;
    string y = initialPosition.Attribute("In_Y").Value;

    Console.WriteLine("Name - {0}.X - {1}.Y - {2}", name, x, y);

}

Console.ReadKey();

You could use XDocument and do this.

    XDocument doc = XDocument.Parse(input);


    var results = doc.Descendants("Pedestrian")
                     .Select(x=> 
                             new Pedestrian() 
                             {
                                 Name = x.Attribute("Name").Value, 
                                 X1 = int.Parse(x.Element("Initial_Position").Attribute("In_X").Value),
                                 Y1 = int.Parse(x.Element("Initial_Position").Attribute("In_Y").Value),
                                 X2 = int.Parse(x.Element("Final_Position").Element("First") .Attribute("Fin_X").Value),
                                 Y2 = int.Parse(x.Element("Final_Position").Element("First") .Attribute("Fin_Y").Value)
                             });

Output

 Name  : Mother
 X1    : 3
 X2    : 6
 Y1    : 3
 Y2    : 6

Check this Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM