2014-02-16 145 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

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