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

时间:2015-07-07 20:09:14

标签: c# xml linq-to-xml

我想检查地址子节点传递/发票是否存在。有没有一种简单的方法来实现这一目标?以下是解析字典(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 doc示例:

<?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>

1 个答案:

答案 0 :(得分:1)

为了检查,您需要对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