使用另一个元素

时间:2016-04-23 21:28:03

标签: c# xml linq

我正在尝试使用另一个元素的值来获取其中一个后代元素的值。

以下是XML的外观:

<RateQuoteResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <RateQuote>
    <SERVICEUPGRADES>
      <DELIVERYTIME>Single-hour Window</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed window</SERVICE_TYPE>
      <TOTAL_COST>245.52</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYTIME>Multi-hour Window</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed window</SERVICE_TYPE>
      <TOTAL_COST>245.52</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDAYS>Please Call Customer Service for Available Days and Times</DELIVERYDAYS>
      <DELIVERYTIME>Single or Multi Day Window</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed window</SERVICE_TYPE>
      <TOTAL_COST>130.52</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDATE>04/26/2016</DELIVERYDATE>
      <DELIVERYDAYS>1</DELIVERYDAYS>
      <DELIVERYTIME>before 9:00 AM</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed delivery</SERVICE_TYPE>
      <TOTAL_COST>195.52</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDATE>04/26/2016</DELIVERYDATE>
      <DELIVERYDAYS>1</DELIVERYDAYS>
      <DELIVERYTIME>before 12:00 PM (noon)</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed delivery</SERVICE_TYPE>
      <TOTAL_COST>160.52</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDATE>04/26/2016</DELIVERYDATE>
      <DELIVERYDAYS>1</DELIVERYDAYS>
      <DELIVERYTIME>before 3:30 PM</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed delivery</SERVICE_TYPE>
      <TOTAL_COST>130.52</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDATE>04/26/2016</DELIVERYDATE>
      <DELIVERYDAYS>1</DELIVERYDAYS>
      <SERVICE_TYPE>regional delivery</SERVICE_TYPE>
      <TOTAL_COST>95.52</TOTAL_COST>
    </SERVICEUPGRADES>
  </RateQuote>
</RateQuoteResponse>

我要做的是获取TOTAL_COST节点的值,其中元素为regional delivery

以下代码适用于所有其他元素:

var deliveryTime1 = doc.Root.Descendants("SERVICEUPGRADES").SingleOrDefault(c => (string)c.Element("DELIVERYTIME") == "Single-hour Window");

var deliveryTime2 = doc.Root.Descendants("SERVICEUPGRADES").SingleOrDefault(c => (string)c.Element("DELIVERYTIME") == "Multi-hour Window");

var deliveryTime3 = doc.Root.Descendants("SERVICEUPGRADES").SingleOrDefault(c => (string)c.Element("DELIVERYTIME") == "Single or Multi Day Window");

所有这些都有效,但下面的行每次都返回null,即使节点存在并且包含具有值的元素。

var deliveryTime7 = doc.Root.Descendants("SERVICEUPGRADES").SingleOrDefault(c => (string)c.Element("SERVICE_TYPE") == "regional delivery");

在尝试查找此特定元素时,我是否做错了什么?我已经尝试添加名称空间(如下所示),但这也不起作用。

var deliveryTime7 = doc.Root.Descendants(ns + "SERVICEUPGRADES").SingleOrDefault(e => (string)e.Element(ns + "SERVICE_TYPE") == "regional delivery");

2 个答案:

答案 0 :(得分:0)

添加命名空间不是必需的,因为您的内容不在命名空间中(据我们所见)。但是你应该有所作为。

我可以给你的唯一建议是在使用SingleOrDefault()时要小心,如果有多个结果,这将取决于您查询的服务类型,可能会发生。

var query =
    from e in doc.Descendants("SERVICEUPGRADES")
    where (string)e.Element("SERVICE_TYPE") == "regional delivery"
    select (decimal)e.Element("TOTAL_COST");

答案 1 :(得分:0)

我尝试了你的代码,它就像一个魅力。您还可以使用XPath获取所需的元素,如下所示:

string deliveryTotalCost = "//SERVICEUPGRADES[SERVICE_TYPE='regional delivery']/TOTAL_COST";

var cost = doc.XPathSelectElement(deliveryTotalCost);

Console.WriteLine(cost.Value);

将打印95.52

我个人认为它更具可读性,尤其是当查询变得更复杂时。 Linq非常强大,特别是对于DAL和List操作,但对于XML解析我不是一个大粉丝。

祝你好运