我正面临一个问题,即使用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>
答案 0 :(得分:1)
尝试将命名空间添加到SERVICE_TYPE:
"//rq:SERVICEUPGRADES[rq:SERVICE_TYPE='regional delivery']//rq:TOTAL_COST"
答案 1 :(得分:0)
LinqToXml相当容易,因为显式强制转换运算符可以轻松实现所有类型转换:
XNamespace ns="http://ratequote.usfnet.usfc.com/v2/x1";
var doc = XDocument.Parse(xml); //or whatever
var costNode = doc
.Root
.Descendants(ns + "SERVICEUPGRADES")
.Single(e => (string)e.Element(ns + "SERVICE_TYPE") == "regional delivery")
.Element(ns + "TOTAL_COST");
var cost = (decimal)costNode;