linq到xml查询所有属性

时间:2013-03-11 07:58:15

标签: c# linq linq-to-xml

我有一个xml这样的文件

<Products>
  <Product Name="Mobile Device" Code="10000000000000000006">
    <Providers>
        <Provider Name="VODA" Code="VODA">
            <Menus>
                <Menu  Name="Home" Url="TopModelsNew.aspx" DisplayOrder="1" NewWindow="0"/>
                <Menu  Name="Top Queries" Url="TopIssues.aspx" DisplayOrder="2" NewWindow="0"/>
                <Menu  Name="Error Codes" Url="PCErrors.aspx" DisplayOrder="3" NewWindow="0"/>
            </Menus>
        </Provider>
    </Providers>
</Product>

我想在列表中找到Product Code =“10000000000000000006”和Provider Code =“VODA”的所有菜单。
结果应该是这样的

  Name    Url            DisaplayOrder 
  Home    TopModels      0

我正在尝试使用linq到xml,如下所示

 XDocument xdoc = XDocument.Load(Server.MapPath("~/App_Data/LeftMenu.xml"));
        var products = from product in xdoc.Descendants("Product") where
        new{
                 .....    
            };

我无法找到如何获取列表。

3 个答案:

答案 0 :(得分:2)

XDocument xdoc = XDocument.Load(Server.MapPath("~/App_Data/LeftMenu.xml"));
var menus = from product in xdoc.Descendants("Product")
            where (string)product.Attribute("Code") == "10000000000000000006"
            from provider in product.Descendants("Provider")
            where (string)provider.Attribute("Code") == "VODA"
            from menu in provider.Descendants("Menu")
            select new { 
                Name = (string)menu.Attribute("Name"),
                Url = (string)menu.Attribute("Url"),
                DisplayOrder = (int)menu.Attribute("DisplayOrder")
            };

结果:

  Name            Url                  DisaplayOrder 
  "Home"          "TopModelsNew.aspx"       1
  "Top Queries"   "TopIssues.aspx"          2
  "Error Codes"   "PCErrors.aspx"           3

答案 1 :(得分:2)

var menus = doc.Root
                .Elements("Product")
                .Where(p => (string)p.Attribute("Code") == "10000000000000000006")
                .SelectMany(p => p.Element("Providers").Elements("Provider"))
                .Where(p => (string)p.Attribute("Code") == "VODA")
                .SelectMany(p => p.Element("Menus").Elements("Menu"))
                .ToList();

它返回样本数据的所有3个菜单元素。您可以使用投影来获取匿名对象而不是XElement

var menus = doc.Root
               .Elements("Product")
               .Where(p => (string)p.Attribute("Code") == "10000000000000000006")
               .SelectMany(p => p.Element("Providers").Elements("Provider"))
               .Where(p => (string)p.Attribute("Code") == "VODA")
               .SelectMany(p => p.Element("Menus").Elements("Menu"))
               .Select(m => new {
                   Name = (string)m.Attribute("Name"),
                   Url = (string)m.Attribute("Url"),
                   DisplayOrder = (int)m.Attribute("DisplayOrder")
               })

答案 2 :(得分:1)

XDocument xdoc = XDocument.Load(Server.MapPath("~/App_Data/LeftMenu.xml"));
var menus = xdoc.Descendants("Menu").Where(x =>
    x.Ancestors("Product").First().Attribute("Code").Value == "10000000000000000006" &&
    x.Ancestors("Provider").First().Attribute("Code").Value == "VODA");

对源XML文档做了一些假设:

  • <Menu>节点必须且只有一个<Product>祖先节点
  • <Menu>节点必须且只有一个<Provider>祖先节点