如何使用C#读取XML文件中节点内的元素值

时间:2016-05-29 09:18:34

标签: c# xml visual-studio

早上好,     我有一个以这种方式写成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>

我实现了一个Class来读取文件。

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++;
}

哪个能够读取任何属性(在这种情况下&#34; 名称&#34;)但是无法在节点内读取然后获取属性(在这种情况下在内部) &#34; Initial_Position &#34;然后&#34; In_X &#34;)。

此外,Pd[i]=new Pedestrian (name, X1, Y1, X2, Y2);行给出了以下错误:

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

3 个答案:

答案 0 :(得分:0)

确保提供所需的XML。确保XML的每个标记都有一个关闭标记, 确保<Pedestrian Name="Mother">有一个结束标记。 然后在执行

之前检查X和Y
Pd[i]=new Pedestrian (name, X1, Y1, X2, Y2);
Pd[i].Draw();

看看Loading XML String into datatable

答案 1 :(得分:0)

为什么不在LINQ to XML中执行此操作。它更简单,代码也更清晰:

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

答案 2 :(得分:0)

您可以使用XDocument并执行此操作。

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

<强>输出

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

选中此Demo