使用 "Linq to XML" 尝试从 XElement 访问多个元素

Using "Linq to XML" Try to access multiple element from XElement

我尝试使用 Linq to XML 解析 XML 失败。尽管正如您在下面看到的 ItemList[0],我可以得到 ItemList 有很多 XML 列表 ItemList (variable) 的元素项,结果只显示一个 XML ItemList (variable)ItemList [0] 的元素。我需要打印出 ItemList [n].

上的所有元素

主要代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace WPF_ParsingXML.ViewModels.Helper
{
    public class XMLHelper
    {
        public void ParseXML_Sample1()
        {
            // Read a file
            XElement root = XElement.Load(Environment.CurrentDirectory + @"\..\..\Sample.xml");

            // Assign the default namespace
            XNamespace aw = "http://www.adventure-works.com";

            // Get values from elements
           IEnumerable<XElement> ItemList =
                from el in root.Elements(aw + "PurchaseOrder")
                select el.Element(aw + "Items");
          
            // Print
            foreach (XElement el in ItemList)
            {
                Console.WriteLine("###################################");
                Console.WriteLine((string)el.Element(aw + "Item").Element(aw + "ProductName"));
                Console.WriteLine((string)el.Element(aw + "Item").Attribute(aw + "PartNumber"));
                Console.WriteLine("###################################");

            }    
        }
    }
}

Sample.xml

<?xml version="1.0" encoding="utf-8"?>
<aw:PurchaseOrders xmlns:aw="http://www.adventure-works.com">
    <aw:PurchaseOrder aw:PurchaseOrderNumber="99503" aw:OrderDate="1999-10-20">
        <aw:Address aw:Type="Shipping">
            <aw:Name>Ellen Adams</aw:Name>
            <aw:Street>123 Maple Street</aw:Street>
            <aw:City>Mill Valley</aw:City>
            <aw:State>CA</aw:State>
            <aw:Zip>10999</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:Address aw:Type="Billing">
            <aw:Name>Tai Yee</aw:Name>
            <aw:Street>8 Oak Avenue</aw:Street>
            <aw:City>Old Town</aw:City>
            <aw:State>PA</aw:State>
            <aw:Zip>95819</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:DeliveryNotes>Please leave packages in shed by driveway.</aw:DeliveryNotes>
        <aw:Items>
            <aw:Item aw:PartNumber="872-AA">
                <aw:ProductName>Lawnmower</aw:ProductName>
                <aw:Quantity>1</aw:Quantity>
                <aw:USPrice>148.95</aw:USPrice>
                <aw:Comment>Confirm this is electric</aw:Comment>
            </aw:Item>
            <aw:Item aw:PartNumber="926-AA">
                <aw:ProductName>Baby Monitor</aw:ProductName>
                <aw:Quantity>2</aw:Quantity>
                <aw:USPrice>39.98</aw:USPrice>
                <aw:ShipDate>1999-05-21</aw:ShipDate>
            </aw:Item>
        </aw:Items>
    </aw:PurchaseOrder>
    <aw:PurchaseOrder aw:PurchaseOrderNumber="99505" aw:OrderDate="1999-10-22">
        <aw:Address aw:Type="Shipping">
            <aw:Name>Cristian Osorio</aw:Name>
            <aw:Street>456 Main Street</aw:Street>
            <aw:City>Buffalo</aw:City>
            <aw:State>NY</aw:State>
            <aw:Zip>98112</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:Address aw:Type="Billing">
            <aw:Name>Cristian Osorio</aw:Name>
            <aw:Street>456 Main Street</aw:Street>
            <aw:City>Buffalo</aw:City>
            <aw:State>NY</aw:State>
            <aw:Zip>98112</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:DeliveryNotes>Please notify me before shipping.</aw:DeliveryNotes>
        <aw:Items>
            <aw:Item aw:PartNumber="456-NM">
                <aw:ProductName>Power Supply</aw:ProductName>
                <aw:Quantity>1</aw:Quantity>
                <aw:USPrice>45.99</aw:USPrice>
            </aw:Item>
        </aw:Items>
    </aw:PurchaseOrder>
    <aw:PurchaseOrder aw:PurchaseOrderNumber="99504" aw:OrderDate="1999-10-22">
        <aw:Address aw:Type="Shipping">
            <aw:Name>Jessica Arnold</aw:Name>
            <aw:Street>4055 Madison Ave</aw:Street>
            <aw:City>Seattle</aw:City>
            <aw:State>WA</aw:State>
            <aw:Zip>98112</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:Address aw:Type="Billing">
            <aw:Name>Jessica Arnold</aw:Name>
            <aw:Street>4055 Madison Ave</aw:Street>
            <aw:City>Buffalo</aw:City>
            <aw:State>NY</aw:State>
            <aw:Zip>98112</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:Items>
            <aw:Item aw:PartNumber="898-AZ">
                <aw:ProductName>Computer Keyboard</aw:ProductName>
                <aw:Quantity>1</aw:Quantity>
                <aw:USPrice>29.99</aw:USPrice>
            </aw:Item>
            <aw:Item aw:PartNumber="898-AM">
                <aw:ProductName>Wireless Mouse</aw:ProductName>
                <aw:Quantity>1</aw:Quantity>
                <aw:USPrice>14.99</aw:USPrice>
            </aw:Item>
        </aw:Items>
    </aw:PurchaseOrder>
</aw:PurchaseOrders>

项目列表[0]

<aw:Items xmlns:aw="http://www.adventure-works.com">
  <aw:Item aw:PartNumber="872-AA">
    <aw:ProductName>Lawnmower</aw:ProductName>
    <aw:Quantity>1</aw:Quantity>
    <aw:USPrice>148.95</aw:USPrice>
    <aw:Comment>Confirm this is electric</aw:Comment>
  </aw:Item>
  <aw:Item aw:PartNumber="926-AA">
    <aw:ProductName>Baby Monitor</aw:ProductName>
    <aw:Quantity>2</aw:Quantity>
    <aw:USPrice>39.98</aw:USPrice>
    <aw:ShipDate>1999-05-21</aw:ShipDate>
  </aw:Item>
</aw:Items>

结果

###################################
Lawnmower
872-AA
###################################
###################################
Power Supply
456-NM
###################################
###################################
Computer Keyboard
898-AZ
###################################

预期结果

###################################
Lawnmower
872-AA
###################################
###################################
Baby Monitor
926-AA
###################################
Power Supply
456-NM
###################################
###################################
Computer Keyboard
898-AZ
###################################
###################################
Wireless Mouse
898-AM
###################################

如果您阅读 Element 的文档,它说:

Gets the first (in document order) child element with the specified XName.

请注意“第一个 child”是怎么写的。在你的情况下你想要检索所有 children,所以你需要调用 Elements.

为此,在当前循环中为每个循环添加一个循环。

foreach (XElement el in ItemList)
{
    foreach (XElement elem in el.Elements(aw + "Item")
    {
        // Rest of code left as exercise for reader
    }

}

作为旁注,如果您只有 objective 正在获取 Items 节点,请考虑调用 Descendants

更好更简单的方法。

c#

void Main()
{
    const string filename = @"e:\Temp\Sample.xml";

    XDocument xdoc = XDocument.Load(filename);
    XNamespace aw = "http://www.adventure-works.com";

    foreach (XElement el in xdoc.Descendants(aw + "Item"))
    {
        Console.WriteLine("###################################");
        Console.WriteLine(el.Element(aw + "ProductName").Value);
        Console.WriteLine(el.Attribute(aw + "PartNumber").Value);
        Console.WriteLine("###################################");
    }
}