gpt4 book ai didi

c# - 具有所有属性的 linq to xml 查询

转载 作者:行者123 更新时间:2023-12-02 17:45:28 26 4
gpt4 key购买 nike

我有一个像这样的 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>

我想在列表中找到产品代码=“10000000000000000006”和供应商代码=“VODA”的所有菜单。
结果应该是这样的

  Name    Url            DisaplayOrder   Home    TopModels      0

I was trying linq to xml as follows

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

我找不到如何获取列表。

最佳答案

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

关于c# - 具有所有属性的 linq to xml 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15333674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com