检索 XML 部件 uing Linq to XML

本文关键字:XML to Linq 部件 检索 uing | 更新日期: 2023-09-27 18:34:51

下面的XML片段是更大的XML的一部分。我一遍又一遍地尝试获取地址行 1、2、3 以及城市和州、邮政编码和国家/地区。我想将其功能化,以便我可以挑选 那些基于InvoiceHeader id="XXXX"的地址,但是我总是碰壁。我已经尝试了下面的查询或类似的查询,但我不断收到未设置为对象实例的错误对象引用。

这是我的问题,有人可以指出我明显的错误。

 IEnumerable<string> partNos =
            from item in PurchaseOrderXml.Descendants("RemitTo").Descendants("Address")
            where (string)item.Attribute("id").Value == "23951768"
            select (string)item;

<Invoice>
    <InvoiceHeader id="23951768" status="InProcess">
        <InvoiceName />
        <InvoiceNumber>23951768</InvoiceNumber>
        <InvoiceDate>2014-09-26 00:00:00.0</InvoiceDate>
        <DueDate>2014-10-26 00:00:00.0</DueDate>
        <SupplierInvoiceNo>534254504</SupplierInvoiceNo>
        <InvoiceType>Invoice</InvoiceType>
      <Supplier id="3825405">
        <ContactInfo type="main">
              <Address>
                <AddressLine lineNumber="1">Post </AddressLine>
                <AddressLine lineNumber="2">30 Street</AddressLine>
                <AddressLine lineNumber="3">30 Street</AddressLine>
                <City>Saint Louis</City>
                <State>MO</State>
                <PostalCode>63103-2530</PostalCode>
                <Country isoCountryCode="US">United States</Country>
            </Address>
        </ContactInfo>
    </Supplier>
        <BillTo>
        <Address>
            <AddressLine lineNumber="1">vvvv</AddressLine>
            <AddressLine lineNumber="2">vvvv</AddressLine>
            <City>Philadelphia</City>
            <State>PA</State>
            <PostalCode>19222</PostalCode>
            <Country isoCountryCode="US">United States</Country>
        </Address>
          </BillTo>
        <RemitTo>
            <Address>
                <AddressLine lineNumber="1">P O BOX 535182</AddressLine>
                <AddressLine lineNumber="2" />
                <AddressLine lineNumber="3" />
                <City>ATLANTA</City>
                <State>GA</State>
                <PostalCode>303535182</PostalCode>
                <Country isoCountryCode="US">United States</Country>
            </Address>
        </RemitTo>
     </InvoiceHeader>
</Invoice>

检索 XML 部件 uing Linq to XML

您的商品范围变量对应于没有 id 属性的 Address 元素。相反,您需要一个查询,该查询首先查找(或筛选(相应的 InvoiceHeader,然后查找匹配的 InvoiceHead 的地址元素结尾。

下面是查找 InvoiceHead 的示例:

var Header = PurchaseOrderXml.Descendants("InvoiceHeader")
.FirstOrDefault(header => (string)header.Attribute("id").Value == headerId);

您可以检查是否找到标头(Header != null(。 拥有标头后,在该给定元素的范围内执行任何需要的操作。例:

var RemitToAddress = Header.Descendants("RemitTo").Descendants("Address").FirstOrDefault();

您可能想要检查标头中的其他元素,因此将查询拆分为这样的部分可以使您的意图清晰。

另请注意,我使用了Descendants,但如果它与您的架构更匹配,您也可以使用 Elements

另一个示例,要获取AddressLine元素并将它们连接起来,您可以尝试如下方法:

IEnumerable<string> AddressLines = RemitToAddress.Elements("AddressLine")
.OrderBy(line => (int)line.Attribute("lineNumber"))
.Select(line => line.Value);
var AddressText = string.Join("'n", AddressLines);

试试这个:-

var result = xdoc.Root.Descendants("InvoiceHeader")
    .Where(x => x.Attribute("id").Value == "23951768")
    .SelectMany(x => x.Descendants("Address"))
    .Select(x =>
    {
       {
         var addressLine1 = x.Elements("AddressLine")
                        .FirstOrDefault(z => z.Attribute("lineNumber").Value == "1");
         var addressLine2 = x.Elements("AddressLine")
                        .FirstOrDefault(z => z.Attribute("lineNumber").Value == "2");
         var addressLine3 = x.Elements("AddressLine")
                        .FirstOrDefault(z => z.Attribute("lineNumber").Value == "3");
         return new
         {
            AddressLine1 = addressLine1 != null ? addressLine1.Value : String.Empty,
            AddressLine2 = addressLine2 != null ? addressLine1.Value : String.Empty,
            AddressLine3 = addressLine3 != null ? addressLine1.Value : String.Empty,
            City = x.Element("City").Value,
            State = x.Element("State").Value
            PostalCode = x.Element("PostalCode").Value,
            Country= x.Element("Country").Value,
          };
        }
    });