通过linq获取子元素到xml

时间:2012-08-29 13:00:37

标签: xml c#-4.0 linq-to-xml

如何从以下xml格式的文件中获取子元素

<?xml version="1.0" encoding="UTF-8"?>
<UserMenu Type="Company">
  <li>Credit Card Details</li>
  <li>Account Details</li>
  <li>Disc Usage</li>
  <li>Billing</li>
</UserMenu>

我希望输出为:

“信用卡详情” “帐户详细资料” “光盘使用” “结算”

1 个答案:

答案 0 :(得分:1)

var doc = XDocument.Load(fileName);
var lis = (from e in doc.Descendants("li")
          where e.Parent.Attribute("Type").Value == "Company"
          select e.Value).ToArray();