使用复杂筛选的 C# 查询 "Linq to XML"

C# Queries "Linq to XML" with complex filtering

因为我是 Linq to XML 的新手,所以我练习使用 MS official document。我想要 PurchaseOrderNumber="99504" and Item PartNumber="898-AZ" and ProductName 的结果 Computer Keyboard。你能给个指导吗?

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

namespace ParsingXML_220211
{
    public class LINQtoXML
    {
        public LINQtoXML()
        {

        }

        public void ParsingXML(string path)
        {        
            XElement root = XElement.Load(path);
            IEnumerable<XElement> purchaseOrders =
                from po in root.Elements("PurchaseOrder")
                where (from add in po.Elements("Address")
                       where (string)add.Attribute("Type") == "Billing" &&
                             (string)add.Element("City") == "Buffalo"
                       select add)
                    .Any()
                select po;
            foreach (XElement po in purchaseOrders)
            {
                var result = from item in po.Elements("Items")
                             where (string)item.Element("item").Attribute("PartNumber") == "898-AZ"
                             select item;
                Console.WriteLine( result.Elements("ProductName").ToString());
            }

        }
    }
}

主要

using System;
using System.Diagnostics;
using System.Xml;

namespace ParsingXML_220211
{
    public class Program
    {
        //https://kibbomi.tistory.com/188
        static void Main(string[] args)
        {
            //Linq to XML
            LINQtoXML linQtoXml
                = new LINQtoXML();
            linQtoXml.ParsingXML("./../../../LinqToXML.xml");

        }
    }
}

LinqToXML.xml


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

这里打错了,应该是“Item”

where (string)item.Element("item").Attribute("PartNumber") == "898-AZ"

如果你只想要“Computer Keyboard”这个词,添加一个foreach

var result = from item in po.Elements("Items")
                             where (string)item.Element("Item").Attribute("PartNumber") == "898-AZ"
                             select item.Element("Item");
                foreach (XElement item in result)
                {
                    Console.WriteLine((string)item.Element("ProductName"));
                }