0

我正在学习linq。我有一个像下面这样的 Xelement

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

如何获取 id 的值作为逗号分隔的字符串

4

2 回答 2

0
XElement xe = XElement.Parse(xmlString);

var ids = 
    String
    .Join
    (
        ",",
        xe
        .Elements("Orders")
        .Elements("param")
        .Select(x=>x.Attribute("id").Value)
        .ToArray()
    );
于 2013-10-08T06:09:31.077 回答
0
var ids = XElement.Load(xmlString)
    .Descendants("param")
    .Select(e => e.Attribute("id").Value);
var result = String.Join(",", ids);
于 2013-10-08T06:11:57.623 回答