0

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

这是我在 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']");

这是我从 Web 服务获得的 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>
4

1 回答 1

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);
于 2013-07-17T01:27:43.937 回答