C# XDocument-表达式帮助
我试图熟悉使用C# XDocument-表达式帮助,c#,.net,linq,linq-to-xml,C#,.net,Linq,Linq To Xml,我试图熟悉使用XDocumentAPI使用lambda语法解析XML 我想做的是基于以下XML结构创建一个IEnumerable产品列表。我知道如何获取所有产品节点,但我想获取每个产品节点的名称属性,然后从“产品”节点中选择每个项目节点,并解析其值 因此,我想采用以下XML: <products> <product name="Prod1"> <item hwid="abk9184"> <href>Prod1/abk9184
XDocument
API使用lambda语法解析XML
我想做的是基于以下XML结构创建一个IEnumerable
产品列表。我知道如何获取所有产品
节点,但我想获取每个产品节点的名称
属性,然后从“产品”节点中选择每个项目
节点,并解析其值
因此,我想采用以下XML:
<products>
<product name="Prod1">
<item hwid="abk9184">
<href>Prod1/abk9184_en-us/abk9184.html</href>
<localization>en-us</localization>
<build.start>2011-06-08 22:02 PM</build.start>
<build.icp>9.0.192.32</build.icp>
</item>
<item hwid="abk9185">
<href>Prod1/abk9185_en-us/abk9185.html</href>
<localization>en-us</localization>
<build.start>2011-06-08 22:03 PM</build.start>
<build.icp>9.0.192.32</build.icp>
</item>
</product>
<product name="Prod2">
<item hwid="aa6410">
<href>Prod2/aa6410_en-us/aa6410.html</href>
<localization>en-us</localization>
<build.start>2011-06-08 22:04 PM</build.start>
<build.icp>9.0.192.32</build.icp>
</item>
</product>
</products>
因此,尽管我有两个产品节点,但最终每个节点都会有许多产品实例。我想学习如何使用XDocument
和lambda语法来实现这一点。有人能给我指路吗
IEnumerable<Product> products = xDocument.Decendants("product")
.Select(e => new Product { Name = e.Name })
IEnumerable products=xDocument.decentants(“产品”)
.Select(e=>新产品{Name=e.Name})
但是我想这里必须有一些循环,才能从每个产品中获得每个项目
var products = document.Descendants("item")
.Select(arg =>
new Product
{
Name = arg.Parent.Attribute("name").Value,
Hwid = arg.Attribute("hwid").Value,
Href = arg.Element("href").Value,
Localization = arg.Element("localization").Value,
BuildDateTime = DateTime.Parse(arg.Element("build.start").Value),
IcpBuildVersion = arg.Element("build.icp").Value
})
.ToList();
var products = document.Descendants("item")
.Select(arg =>
new Product
{
Name = arg.Parent.Attribute("name").Value,
Hwid = arg.Attribute("hwid").Value,
Href = arg.Element("href").Value,
Localization = arg.Element("localization").Value,
BuildDateTime = DateTime.Parse(arg.Element("build.start").Value),
IcpBuildVersion = arg.Element("build.icp").Value
})
.ToList();