【问题标题】:Get Child elements through linq to xml通过linq to xml获取子元素
【发布时间】:2012-08-29 13:00:37
【问题描述】:

如何从以下 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>

我想输出为:

“信用卡详细信息” “帐户详细资料” “光盘使用” “计费”

【问题讨论】:

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


    【解决方案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();
    

    【讨论】:

    • 谢谢。我得到了需要的东西。我很感激。
    • 一个小问题,如果我想添加基于 UserMenu 类型属性的 where 子句,比如 where (string)el.Attribute("Type") == userType
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    相关资源
    最近更新 更多