从xelement获取值

时间:2013-10-08 06:04:51

标签: xml linq entity-framework

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

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

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

2 个答案:

答案 0 :(得分:0)

XElement xe = XElement.Parse(xmlString);

var ids = 
    String
    .Join
    (
        ",",
        xe
        .Elements("Orders")
        .Elements("param")
        .Select(x=>x.Attribute("id").Value)
        .ToArray()
    );

答案 1 :(得分:0)

var ids = XElement.Load(xmlString)
    .Descendants("param")
    .Select(e => e.Attribute("id").Value);
var result = String.Join(",", ids);