簡體   English   中英

如何使用C#讀取XML文件中節點內的元素值

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

早上好,我以這種方式將模板文件編寫為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++;
}

這是能夠讀取任何屬性(在這種情況下,“ 名稱 ‘),但沒有能夠在節點內讀出,然后把屬性(在這種情況下,’Initial_Position”,然后“In_X”內)。

此外, Pd[i]=new Pedestrian (name, X1, Y1, X2, Y2); 給我以下錯誤:

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

確保提供所需的XML。 確保XML的每個標簽都有結束標簽,確保<Pedestrian Name="Mother">有結束標簽。 然后在執行之前檢查X和Y

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

看看將XML字符串加載到datatable中

您為什么不在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();

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM