C# 如何使用LINQ查询复杂的XML文档?

C# 如何使用LINQ查询复杂的XML文档?,c#,xml,linq,C#,Xml,Linq,我不熟悉XML和LINQ,但我试图实现的是将此XML转换为“product lineitem”类型的列表,其中包含两个字段,一个用于净价,一个用于产品id 所以在C#中,这是 List<ProductLineItem> 下面是XML文件的一个示例 <?xml version="1.0" encoding="UTF-8"?> <orders xmlns="xyz"> <order order-no="00000605">

我不熟悉XML和LINQ,但我试图实现的是将此XML转换为“product lineitem”类型的列表,其中包含两个字段,一个用于净价,一个用于产品id

所以在C#中,这是

List<ProductLineItem>
下面是XML文件的一个示例

<?xml version="1.0" encoding="UTF-8"?>
    <orders xmlns="xyz">
        <order order-no="00000605">
            <order-date>2016-04-25T13:45:14.133Z</order-date>
            <created-by>storefront</created-by>
            <original-order-no>00000605</original-order-no>
            <product-lineitems>
                <product-lineitem>
                    <net-price>57.75</net-price>
                    <product-id>3210</product-id>
                </product-lineitem>
                <product-lineitem>
                    <net-price>55.00</net-price>
                    <product-id>5543</product-id>
                </product-lineitem>
                <product-lineitem>
                    <net-price>57.75</net-price>
                    <product-id>4987</product-id>
                </product-lineitem>
            </product-lineitems>
        </order>
        <order order-no="00000622">
            ...
        </order>
        <order order-no="00000666">
            ...
        </order>
    </orders>

这就是你需要的东西:

var ns = XNamespace.Get("xyz");

var productLineItems =
    xd
        .Root
        .Descendants(ns + "product-lineitem")
        .Select(xe => new ProductLineItem()
        {
            ProductId = (int)xe.Element(ns + "product-id"),
            NetPrice = (decimal)xe.Element(ns + "net-price"),
        })
        .ToList();
根据您的样本数据,我得到以下信息:


这正是您需要的东西:

var ns = XNamespace.Get("xyz");

var productLineItems =
    xd
        .Root
        .Descendants(ns + "product-lineitem")
        .Select(xe => new ProductLineItem()
        {
            ProductId = (int)xe.Element(ns + "product-id"),
            NetPrice = (decimal)xe.Element(ns + "net-price"),
        })
        .ToList();
根据您的样本数据,我得到以下信息:


如果有多个订单具有相同的
产品id
,该怎么办?你可以把它们分开吗?如果是这样,听起来您只需要
root.substands(“ProductLineItem”)
来选择它们。不清楚您为什么要使用
StringBuilder
,或者列表将是什么-您是否在某处定义了
LineItem
类?如果有多个订单具有相同的
产品id
,该怎么办?你可以把它们分开吗?如果是这样,听起来您只需要
root.substands(“ProductLineItem”)
来选择它们。现在还不清楚为什么要使用
StringBuilder
进行任何操作,或者列表是什么-您是否在某处定义了
LineItem
类?
XElement root = XElement.Load(fileName);
StringBuilder result = new StringBuilder();
result.AppendLine(element.Attribute("order-no").Value);
foreach (XElement orderElement in root.Elements())
{
    result.AppendLine(orderElement.Attribute("order-no").Value);
    foreach(var item in orderElement.Element("product-lineitems").Elements())
        {
            var i = item.Element("product-id").Value;
        }
}
var ns = XNamespace.Get("xyz");

var productLineItems =
    xd
        .Root
        .Descendants(ns + "product-lineitem")
        .Select(xe => new ProductLineItem()
        {
            ProductId = (int)xe.Element(ns + "product-id"),
            NetPrice = (decimal)xe.Element(ns + "net-price"),
        })
        .ToList();