我有这个XML文件
<?xml version="1.0" encoding="utf-8"?>
<message>
<success/>
<bookings>
<booking>
<rooms>
<room roomCode ="101" uniqueId="abc">
<stays>
<stay usedfrom="9:30" usedto="10:30" quantity="1" Price="62.5" rateCode="1"/>
</stays>
<extras>
<extra from="9:30" to="10:30" unitPrice="5.5" extraCode="coffee" quantity="1" inclusive="0"/>
</extras>
<guests>
<guest firstName="John" lastName="Doe" title="MR" ageRange="0"/>
</guests>
</room>
<room roomCode ="Brd" uniqueId="xyz">
<stays>
<stay usedfrom="13:30" usedto="15:30" quantity="1" unitPrice="60.0000" rateCode="RACK"/>
</stays>
<guests>
<guest firstName="Jean" lastName="Doe" title="MRS" ageRange="0"/>
</guests>
</room>
</rooms>
</booking>
</bookings>
</message>
我正在尝试获取节点,并通过下面的所有(NO s)节点(以及子节点)进行循环。 我已经完成了这个,但它列出了每个循环的所有属性。
foreach (XmlNode Roomnode in RoomS)
{
XmlNodeList Room = doc.GetElementsByTagName("room");
var RoomCount = Room.Count;
Console.WriteLine("This Booking contains " + RoomCount + " room(s)");
foreach (XmlNode rmxn in Room)
{
RoomCode = rmxn.Attributes["roomCode"].Value;
UniqueId = rmxn.Attributes["uniqueId"].Value;
Console.WriteLine(" Room Type: " + RoomCode);
Console.WriteLine(" Room id: " + UniqueId);
XmlNodeList Stay = doc.GetElementsByTagName("stay");
foreach (XmlNode syxn in Stay)
{
RateCode = syxn.Attributes["rateCode"].Value;
Quantity = syxn.Attributes["quantity"].Value;
UnitPrice = Math.Round(Convert.ToDecimal(syxn.Attributes["unitPrice"].Value), 2);
FromTime = syxn.Attributes["usedfrom"].Value;
ToTime = syxn.Attributes["usedto"].Value;
Console.WriteLine(" Staying from " + FromTime + " to " + ToTime);
Console.WriteLine(" Price Per Day : " + Currency + UnitPrice);
Console.WriteLine(" Using Rate Code: " + RateCode);
}
}
}
我(99.99%)确定它是因为行
XmlNodeList Stay = doc.GetElementsByTagName("stay");
现在,在JQUERY中,我会用“this”替换“doc”(而不是再次获取整个列表,但“this”不起作用(来自VS的“静态属性”消息)。 有人可以告诉我我需要做些什么来改变它:
Room Type: 101
Room id: abc
Staying from 9:30 to 10:30
Price Per Day : 62.5
Using Rate Code: 1
Staying from 13:20 to 15:30
Price Per Day : 60.00
Using Rate Code: RACK
Room Type: Brd
Room id: xyz
Staying from 9:30 to 10:30
Price Per Day : 62.5
Using Rate Code: 1
Staying from 13:20 to 15:30
Price Per Day : 60.00
Using Rate Code: RACK
到这个
Room Type: 101
Room id: abc
Staying from 9:30 to 10:30
Price Per Day : 62.5
Using Rate Code: 1
Room Type: Brd
Room id: xyz
Staying from 13:20 to 15:30
Price Per Day : 60.00
Using Rate Code: RACK
由于
答案 0 :(得分:1)
您要求所有stay
元素,而不是当前room
中的元素:
XmlNodeList Stay = doc.GetElementsByTagName("stay");
应该是
XmlNodeList Stay = room.GetElementsByTagName("stay");
答案 1 :(得分:0)
我想我已经修好了我的替换
XmlNodeList Stay = doc.GetElementByTagName("stay") ;
以下两行
XmlNode oNode = rmxn.SelectSingleNode(".//stays");
XmlNodeList Stay = oNode.SelectNodes("stay");
感谢所有帮助过的人,我希望这可以帮助将来遇到同样问题的人。