如何条件查询xml值
XmlDocument doc = new XmlDocument();
doc.LoadXml(Serialize(V2)); //把返回的object对象转为string类型
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("ns", "http://tempus.cn/NorthBoundService"); //xml命名空间
XmlNodeList someBooks = doc.SelectNodes("//ns:RealtimeHotelRoomV2", nsMgr); //没加条件查询RealtimeHotelRoomV2节点
XmlNodeList someBooks2 = doc.SelectNodes("//ns:RealtimeHotelRoomV2[@RoomID='179562']", nsMgr); //查询RealtimeHotelRoomV2节点加了条件
foreach (XmlNode item in someBooks)
{
string name = item.SelectSingleNode("RoomName").Value;
string age = item.SelectSingleNode("RoomID").Value;
PopupMsg.Show_Msg(string.Format("{0}:{1}", name, age));
Response.Write(name + age);
}
这是返回的数据。。
<?xml version="1.0"?>
<RealtimeHotelResponseV2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ResponseHead xmlns="http://tempus.cn/NorthBoundService">
<ResultOfRequest>true</ResultOfRequest>
<ResultMessage>OK</ResultMessage>
<Time>2013-12-19T11:08:25.60375+08:00</Time>
</ResponseHead>
<HotelCount xmlns="http://tempus.cn/NorthBoundService">1</HotelCount>
<HotelPageCount xmlns="http://tempus.cn/NorthBoundService">1</HotelPageCount>
<RealtimeHotels xmlns="http://tempus.cn/NorthBoundService">
<RealtimeHotelV2>
<HotelID>46225</HotelID>
<HotelCityID>10522</HotelCityID>
<HotelName>深圳美逸酒店</HotelName>
<Star>2</Star>
<BrandID>0</BrandID>
<HotelAddress>深圳市福田区益田路3008号皇都广场C座</HotelAddress>
<LowestPrice>0</LowestPrice>
<RealtimeRooms>
<RealtimeHotelRoomV2>
<RoomID>405683</RoomID>
<RoomName>标准单人房(无窗)</RoomName>
<Plans>
<RealtimePlan>
<RoomPlanID>2034563</RoomPlanID>
<PlanName>不含早</PlanName>
<GeustRoomID>405683</GeustRoomID>
<PlanTypeCode>0</PlanTypeCode>
<HotelRates>
<TotalPrice>0</TotalPrice>
<Currency>RMB</Currency>
</HotelRates>
<CommisionRate>0</CommisionRate>
<BackAmount>0</BackAmount>
<RealtimeSuretys />
</RealtimePlan>
</Plans>
</RealtimeHotelRoomV2>
<RealtimeHotelRoomV2>
<RoomID>405684</RoomID>
<RoomName>标准双人房(无窗)</RoomName>
<Plans>
<RealtimePlan>
<RoomPlanID>1191588</RoomPlanID>
<PlanName>不含早</PlanName>
<GeustRoomID>405684</GeustRoomID>
<PlanTypeCode>0</PlanTypeCode>
<HotelRates>
<TotalPrice>0</TotalPrice>
<Currency>RMB</Currency>
</HotelRates>
<CommisionRate>0</CommisionRate>
<BackAmount>0</BackAmount>
<RealtimeSuretys />
</RealtimePlan>
</Plans>
</RealtimeHotelRoomV2>
<RealtimeHotelRoomV2>
<RoomID>405686</RoomID>
<RoomName>豪华双人房</RoomName>
<Plans>
<RealtimePlan>
<RoomPlanID>1191590</RoomPlanID>
<PlanName>不含早</PlanName>
<GeustRoomID>405686</GeustRoomID>
<PlanTypeCode>0</PlanTypeCode>
<HotelRates>
<TotalPrice>0</TotalPrice>
<Currency>RMB</Currency>
</HotelRates>
<CommisionRate>0</CommisionRate>
<BackAmount>0</BackAmount>
<RealtimeSuretys />
</RealtimePlan>
</Plans>
</RealtimeHotelRoomV2>
<RealtimeHotelRoomV2>
<RoomID>426602</RoomID>
<RoomName>商务单人房</RoomName>
<Plans>
<RealtimePlan>
<RoomPlanID>1216636</RoomPlanID>
<PlanName>不含早</PlanName>
<GeustRoomID>426602</GeustRoomID>
<PlanTypeCode>0</PlanTypeCode>
<HotelRates>
<TotalPrice>0</TotalPrice>
<Currency>RMB</Currency>
</HotelRates>
<CommisionRate>0</CommisionRate>
<BackAmount>0</BackAmount>
<RealtimeSuretys />
</RealtimePlan>
</Plans>
</RealtimeHotelRoomV2>
</RealtimeRooms>
</RealtimeHotelV2>
</RealtimeHotels>
</RealtimeHotelResponseV2>
问题就是我查询没加条件的时候能查询到房间数据,加了条件以后就查询不到了。条件的写法应该是对的,求指点 --------------------编程问答-------------------- 你发出来的这个返回的xml里面没有满足RoomID='179562'这个条件的呀。肯定查不出来数据、 --------------------编程问答-------------------- 我引用对的id也查不出来 --------------------编程问答--------------------
XmlNodeList someBooks = doc.SelectNodes("//ns:RealtimeHotelRoomV2", nsMgr); //查询RealtimeHotelRoomV2节点加了条件
foreach (XmlNode item in someBooks)
{
if (item.SelectSingleNode("//ns:RoomID", nsMgr).InnerText == "179562")
{
string RoomID = item.SelectSingleNode("//ns:RoomID", nsMgr).InnerText;
}
}
要不这样操作吧。
补充:.NET技术 , C#