0

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
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
  • What is the `Pedestrian` class? – freefaller May 29 '16 at 09:42
  • I'd suggest you trying some kind of XML deserialization or LINQ to XML. `XmlDocument`-based solutions tend to be much harder to use. – MarcinJuraszek May 29 '16 at 09:43
  • There's some info missing in the code. What is the `Pedestrian`class, what type is `reader` (I'm assuming XmlReader) and where is the `Pd` array defined. Also, the `System.IndexOutOfRangeException` is probably caused by the array not having enough space. – agentshowers May 29 '16 at 09:44

3 Answers3

0

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.

Community
  • 1
  • 1
Muhammad Hassan
  • 475
  • 2
  • 14
0

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();
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
0

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

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35