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
3
  • What is the Pedestrian class? Commented May 29, 2016 at 9: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. Commented May 29, 2016 at 9:43
  • There's some info missing in the code. What is the Pedestrianclass, 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. Commented May 29, 2016 at 9:44

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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();

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.