解析前检查XML个文档子节点是否存在

Check if XML Document child node exists before parsing

我想检查地址子节点 delivery/invoice 是否存在。有没有一种简单的方法可以做到这一点?这是用于解析字典(导入列表)中的 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 执行类似的查询以查看您要查找的元素是否存在,例如:

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