2014-02-16 144 views
0

我得到了這樣的事情:XML多個屬性

<item name="Whatever"> 
    <Point x="12312" y="24234" /> 
    <Point x="242342" y="2142" /> 
</item> 

我需要的,如果該數組包含的名稱和點列表中的陣列來解析這個項目。

我之前沒有真正使用過xml。

這是我的代碼背後到目前爲止

XmlReader reader = XmlReader.Create("Gestures.xml"); 
while (reader.Read()) 
{ 
    KnownGestures temp = new KnownGestures(); 
    IList<Point> GesturePath = new List<Point>(); 
    // Only detect start elements. 
    if (reader.IsStartElement()) 
    { 
     // Get element name and switch on it. 
     switch (reader.Name) 
     { 
      case "Gesture": 
       // Detect this element. 
       temp.GestureName = reader["Name"]; 
       break; 
      case "Point": 
       var XValue = reader["X"]; 
       var YValue = reader["Y"]; 
       Point tempPoint = new Point {X = double.Parse(XValue), Y = double.Parse(YValue)}; 
       GesturePath.Add(tempPoint); 
       temp.GesturePath = GesturePath; 
       break; 
     } 

     GesturesList.Add(temp); 
    } 
} 

編輯

+0

不應該這是開始使用它的好時機?到目前爲止你做了什麼? –

+0

[在c#中從.xml文件中獲取多個屬性]可能的重複(http://stackoverflow.com/questions/15908191/acquiring-multiple-attributes-from-xml-file-in-c-sharp) –

+0

我已經編輯後,也許後面的代碼將幫助 –

回答

2

我發現Linq2Xml更容易使用

var points = XDocument.Load(filename) 
      .Descendants("Point") 
      .Select(p => new Point((int)p.Attribute("x"), (int)p.Attribute("y"))) 
      .ToList(); 
+1

非常感謝,它的工作:) –