我面临使用 XPath 从 XML 中的特定节点提取值的问题。我要做的是根据其他节点中的值从节点获取值。因此,当我查询 XML 时,另一个节点中的值是一个条件。
这是我在 C# 中的 XPath 代码:
我可以使用节点的位置来提取值。
String webURL3;
WebRequest YRCRateQuote;
WebResponse resp3;
Stream respStream3;
XmlDocument rateQuote3 = new XmlDocument();
XmlNode freightChargeSP1;
XmlNode freightChargeSP2;
XmlNode freightChargeSP3;
XmlNode freightChargeSP4;
XmlNode freightChargeSP5;
XmlNode freightChargeSP6;
XmlNode freightChargeSP7;
XmlNode serviceDaysSP;
//build web request
webURL3 = String.Format(web service string);
//create web request
YRCRateQuote = WebRequest.Create(webURL3);
//make web service call
resp3 = YRCRateQuote.GetResponse();
//read response
respStream3 = resp3.GetResponseStream();
rateQuote3.Load(respStream3);
//Use Name Space
XmlNamespaceManager nsmgr = new XmlNamespaceManager(rateQuote3.NameTable);
nsmgr.AddNamespace("rq", "http://ratequote.usfnet.usfc.com/v2/x1");
//This works get the value of the first TOTAL_COST node
freightChargeSP2 = rateQuote3.SelectSingleNode("//rq:SERVICEUPGRADES[1]//rq:TOTAL_COST", nsmgr);
//But this one gives me null if I want to pull the value depending upon value in the other node
freightChargeSP1 = rateQuote3.SelectSingleNode("//rq:SERVICEUPGRADES[SERVICE_TYPE='regional delivery']//rq:TOTAL_COST", nsmgr);
//I also tried doing this
freightChargeSP1 = rateQuote3.SelectSingleNode("//rq:SERVICEUPGRADES//rq:TOTAL_COST[SERVICE_TYPE='regional delivery']", nsmgr);
将 // 更改为 / 也无济于事。
这是 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>