解析前检查XML Document子节点是否存在

本文关键字:子节点 是否 存在 Document XML 检查 | 更新日期: 2023-09-27 18:01:24

我想检查地址子节点交付/发票是否存在。有什么简单的方法可以做到这一点吗?下面是解析字典(importlist)中的xml节点的代码。

if (fileref.importList.ContainsKey("Addresses")) //here I want to check if addresses child node deliveryaddress/invoiceaddress exist
{
var deliveryAddress = order.Elements(ns + "Addresses")
.Elements(ns + "Delivery")
.Select(e => new Invoices.Address
{
Name = (string)e.Element(ns + "Name") == null ? null : (string)e.Element(ns + "Name"),
PostalCode = (string)e.Element(ns + "PostalCode") == null ? null : (string)e.Element(ns + "PostalCode"),
PostalArea = (string)e.Element(ns + "PostalArea") == null ? null : (string)e.Element(ns + "PostalArea"),
State = (string)e.Element(ns + "State") == null ? null : (string)e.Element(ns + "State"),
Street = (string)e.Element(ns + "Street") == null ? null : (string)e.Element(ns + "Street"),
Country = (string)e.Element(ns + "Country") == null ? null : (string)e.Element(ns + "Country"),
City = (string)e.Element(ns + "City") == null ? null : (string)e.Element(ns + "City"),
})
.Single();
var invoiceAddress = order.Elements(ns + "Addresses")
.Elements(ns + "Invoice")
.Select(e => new Invoices.Address
{
Name = (string)e.Element(ns + "Name") == null ? null : (string)e.Element(ns + "Name"),
PostalCode = (string)e.Element(ns + "PostalCode") == null ? null : (string)e.Element(ns + "PostalCode"),
PostalArea = (string)e.Element(ns + "PostalArea") == null ? null : (string)e.Element(ns + "PostalArea"),
State = (string)e.Element(ns + "State") == null ? null : (string)e.Element(ns + "State"),
Street = (string)e.Element(ns + "Street") == null ? null : (string)e.Element(ns + "Street"),
Country = (string)e.Element(ns + "Country") == null ? null : (string)e.Element(ns + "Country"),
City = (string)e.Element(ns + "City") == null ? null : (string)e.Element(ns + "City"),
})
.Single();
Invoices.Addresses addresses = new Invoices.Addresses();
addresses.Delivery = deliveryAddress;
addresses.Invoice = invoiceAddress;
} //end of addresses check

下面是XML文档的例子:

<?xml version="1.0" encoding="utf-8"?>
<InvoiceOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OrderId xmlns="http://24sevenOffice.com/webservices">115</OrderId>
  <CustomerId xmlns="http://24sevenOffice.com/webservices">21</CustomerId>
  <CustomerName xmlns="http://24sevenOffice.com/webservices">James Hertz</CustomerName>
  <Addresses xmlns="http://24sevenOffice.com/webservices">
    <Delivery>
      <Street>11 Shewell Walk</Street>
      <State>CT</State>
      <PostalCode>CO1 1WG</PostalCode>
      <PostalArea />
      <Name />
      <City>Test</City>
      <Country>US</Country>
    </Delivery>
    <Invoice>
      <Street>11 Shewell Walk</Street>
      <State>CT</State>
      <PostalCode>CO1 1WG</PostalCode>
      <PostalArea />
      <Name />
      <City>Test</City>
      <Country>US</Country>
    </Invoice>
  </Addresses>
  <OrderStatus xmlns="http://24sevenOffice.com/webservices">Offer</OrderStatus>
  <DateOrdered xmlns="http://24sevenOffice.com/webservices">2015-06-15T14:00:00Z</DateOrdered>
  <PaymentTime xmlns="http://24sevenOffice.com/webservices">14</PaymentTime>
  <IncludeVAT xsi:nil="true" xmlns="http://24sevenOffice.com/webservices" />
  <OrderTotalIncVat xmlns="http://24sevenOffice.com/webservices">0.0000</OrderTotalIncVat>
  <OrderTotalVat xmlns="http://24sevenOffice.com/webservices">0.0000</OrderTotalVat>
  <Currency xmlns="http://24sevenOffice.com/webservices">
    <Symbol>LOCAL</Symbol>
  </Currency>
  <TypeOfSaleId xmlns="http://24sevenOffice.com/webservices">-100</TypeOfSaleId>
  <InvoiceRows xmlns="http://24sevenOffice.com/webservices">
    <InvoiceRow />
  </InvoiceRows>
</InvoiceOrder>

解析前检查XML Document子节点是否存在

为了进行检查,您需要对XML执行类似的查询,以查看您正在查找的元素是否存在,例如:

var addressesExists = order.Elements(ns + "Addresses").Any()

但是,由于您随后将直接再次执行此查询,因此您可以将Single更改为SingleOrDefault。这样,如果它不存在,那么相关的地址变量将是null(但是,如果有多个地址,它将抛出异常)。此外,如前所述,您的null检查是多余的,可以删除。

将这些联系在一起,您的送货地址查询将如下所示:

var deliveryAddress = order.Elements(ns + "Addresses")
    .Elements(ns + "Delivery")
    .Select(e => new Invoices.Address
    {
        Name = (string)e.Element(ns + "Name"),
        PostalCode = (string)e.Element(ns + "PostalCode"),
        PostalArea = (string)e.Element(ns + "PostalArea"),
        State = (string)e.Element(ns + "State"),
        Street = (string)e.Element(ns + "Street"),
        Country = (string)e.Element(ns + "Country"),
        City = (string)e.Element(ns + "City"),
    }).SingleOrDefault();

如果此路径上没有地址,则为null