2014-01-09 43 views
0
<Shape ID="1" NameU="Start/End" Name="Start/End" Type="Shape" Master="2"> 
....</Shape> 
<Shape ID="2" NameU="Start/End" Name="Start/End" Type="Shape" Master="5"> 
....</Shape> 

我必须返回每个ID值的主值。 我怎样才能实现它通过使用LINQ到XMl。如何使用linq获取xml代码的属性值

回答

0

你真的不存在于您的XML文档的样子,所以我认为这是如下:

<Shapes> 
    <Shape ID="1" NameU="Start/End" Name="Start/End" Type="Shape" Master="2"> 
    </Shape> 
    <Shape ID="2" NameU="Start/End" Name="Start/End" Type="Shape" Master="5"> 
    </Shape> 
</Shapes> 

你可以简单地得到Master属性值都不同ID这样的:

var xDoc = XDocument.Load("Input.xml"); 

var masters = xDoc.Root 
        .Elements("Shape") 
        .ToDictionary(
        x => (int)x.Attribute("ID"), 
        x => (int)x.Attribute("Master") 
       ); 

masters将是Dictionary<int, int>其中关键是您的ID和值是相应的Master属性值。