2013-10-08 95 views
0

我正在學習linq。我有一個的XElement像下面從xelement獲取值

<Customer> 
<Orders> 
    <param id="13" title="Book1" /> 
    <param id="15" title="Book2" /> 
</Orders> 
<Customer/> 

如何獲得id的值作爲一個逗號分隔的字符串

回答

0
XElement xe = XElement.Parse(xmlString); 

var ids = 
    String 
    .Join 
    (
     ",", 
     xe 
     .Elements("Orders") 
     .Elements("param") 
     .Select(x=>x.Attribute("id").Value) 
     .ToArray() 
    ); 
0
var ids = XElement.Load(xmlString) 
    .Descendants("param") 
    .Select(e => e.Attribute("id").Value); 
var result = String.Join(",", ids);