使用XPath提取所需数据但获取空值

时间:2013-07-17 01:07:13

标签: c# xml web-services xpath

使用XPath从XML中提取所需数据时遇到问题。我正在连接到Web服务并以XML格式获取响应。然后我使用XPath在XmlNode中加载数据。我在这段代码中使用C#。我面临的问题是我无法从XML响应中读取与之关联的任何节点和数据。 XmlNode中的值始终为null。 理想情况下,我想提取特定节点的数据。

以下是我在C#代码中执行的操作,用于读取响应和存储节点值。

String webURL3;
WebRequest YRCRateQuote;
WebResponse resp3;
Stream respStream3;
XmlDocument rateQuote3 = new XmlDocument();
XmlNode freightCharge3;
XmlNode serviceDays3;

//build web request
webURL3 = String.Format(webstring);

//create web request
YRCRateQuote = WebRequest.Create(webURL3);
//make web service call
resp3 = YRCRateQuote.GetResponse();

//read response 
respStream3 = resp3.GetResponseStream();
rateQuote3.Load(respStream3);

//use xpath to pull reqd data
freightCharge3 = rateQuote3.SelectSingleNode("//TOTAL_COST");
serviceDays3 = rateQuote3.SelectSingleNode("//SERVICEDAYS");
//I also tried this
freightCharge3 = rateQuote3.SelectSingleNode("//TOTAL_COST[DELIVERYTIME='Multi-hour Window']");

以下是我从网络服务获得的XML响应。

<?xml version="1.0"?>
<RateQuoteResponse xmlns="http://ratequote.usfnet.usfc.com/v2/x1">
  <STATUS>
    <CODE>0</CODE>
    <VIEW>SECURED</VIEW>
    <VERSION>
      <CURRENT>V2X1</CURRENT>
      <CURRENT_RELEASE_DATE>02/06/2010</CURRENT_RELEASE_DATE>
      <LATEST>V2X1</LATEST><LATEST_RELEASE_DATE>02/06/2010</LATEST_RELEASE_DATE>
    </VERSION></STATUS>
  <RateQuote>
    <ORIGIN>
      <NAME>INDIANAPOLIS</NAME>
      <CARRIER>USF Holland, Inc</CARRIER>
      <ADDRESS>2530 S TIBBS AVE</ADDRESS>
      <CITY>INDIANAPOLIS</CITY>
      <STATE>IN</STATE>
      <ZIP>46241</ZIP>
      <PHONE>3172277627</PHONE>
      <PHONE_TOLLFREE>8006274831</PHONE_TOLLFREE>
      <FAX>3172277629</FAX>
    </ORIGIN>
    <DESTINATION>
      <NAME>INDIANAPOLIS</NAME>
      <CARRIER>USF Holland, Inc</CARRIER>
      <ADDRESS>2530 S TIBBS AVE</ADDRESS>
      <CITY>INDIANAPOLIS</CITY>
      <STATE>IN</STATE>
      <ZIP>46241</ZIP>
      <PHONE>3172277627</PHONE>
      <PHONE_TOLLFREE>8006274831</PHONE_TOLLFREE>
      <FAX>3172277629</FAX>
    </DESTINATION>
    <ORIGIN_ZIP>46202</ORIGIN_ZIP>
    <DESTINATION_ZIP>46204</DESTINATION_ZIP>
    <TOTAL_COST>117.02</TOTAL_COST>
    <DISCOUNTPERCENTAGE>85.0</DISCOUNTPERCENTAGE>
    <DISCOUNTAMOUNT>512.04</DISCOUNTAMOUNT>
    <SERVICEDAYS>1</SERVICEDAYS>
    <INDUSTRYDAYS>1.5</INDUSTRYDAYS>
    <CLASSWEIGHT>
      <CLASS>60</CLASS>
      <ASCLASS>60</ASCLASS>
      <WEIGHT>1500</WEIGHT>
      <CHARGES>602.4</CHARGES>
    </CLASSWEIGHT>
    <ADDIONALCHARGES>
      <RATE>Fuel Surcharge</RATE>
      <CHARGES>26.66</CHARGES>
      <DESCRIPTION>FSC29.50% ON DIESEL OF 386.7</DESCRIPTION>
    </ADDIONALCHARGES>
    <SERVICEUPGRADES>
      <DELIVERYDAYS>Please Call Customer Service for Available Days and Times</DELIVERYDAYS>
      <DELIVERYTIME>Single-hour Window</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed window</SERVICE_TYPE>
      <TOTAL_COST>267.02</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDAYS>Please Call Customer Service for Available Days and Times</DELIVERYDAYS>
      <DELIVERYTIME>Multi-hour Window</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed window</SERVICE_TYPE>
      <TOTAL_COST>267.02</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>152.02</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDATE>07/17/2013</DELIVERYDATE>
      <DELIVERYDAYS>1</DELIVERYDAYS>
      <DELIVERYTIME>before 9:00 AM</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed delivery</SERVICE_TYPE>
      <TOTAL_COST>217.02</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDATE>07/17/2013</DELIVERYDATE>
      <DELIVERYDAYS>1</DELIVERYDAYS>
      <DELIVERYTIME>before 12:00 PM (noon)</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed delivery</SERVICE_TYPE>
      <TOTAL_COST>182.02</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDATE>07/17/2013</DELIVERYDATE>
      <DELIVERYDAYS>1</DELIVERYDAYS>
      <DELIVERYTIME>before 3:30 PM</DELIVERYTIME>
      <SERVICE_TYPE>guaranteed delivery</SERVICE_TYPE>
      <TOTAL_COST>152.02</TOTAL_COST>
    </SERVICEUPGRADES>
    <SERVICEUPGRADES>
      <DELIVERYDATE>07/17/2013</DELIVERYDATE>
      <DELIVERYDAYS>1</DELIVERYDAYS>
      <SERVICE_TYPE>regional delivery</SERVICE_TYPE>
      <TOTAL_COST>117.02</TOTAL_COST>
    </SERVICEUPGRADES>
  </RateQuote>
</RateQuoteResponse>

1 个答案:

答案 0 :(得分:1)

您需要考虑命名空间

请参阅:http://msdn.microsoft.com/en-us/library/h0hw012b.aspx

XmlNamespaceManager nsmgr = new XmlNamespaceManager(rateQuote3);
     nsmgr.AddNamespace("rq", "http://ratequote.usfnet.usfc.com/v2/x1");
     XmlNode cost = rateQuote3.SelectSingleNode("//rq:TOTAL_COST", nsmgr);