问题描述
我遍历具有两个列表的XML文档,例如:
< xml>
< nodelist1 A>
< node1></node1>
< node2></node2>
< nodelist2 A>
< node3>
< node4>
</nodelist2>
</nodelist1>
< nodelist1 B>
< node1></node1>
< node2></node2>
< nodelist2 B>
< node3>
< node4>
</nodelist2>
</nodelist1>
</xml>
循环遍历第一个节点列表很好,但是我无法使第二个节点正常工作.
这是一个代码示例:
nodeRooms = xmlDoc.SelectNodes("/test:预订/测试:Rooms/test:Room",ns);
foreach(nodeRooms中的XmlNode nodeRoom)
{
objWriter.WriteStartElement("Room");
objWriter.WriteStartElement("RoomTypeCode");
objWriter.WriteString(nodeRoom ["RoomTypeCode"].InnerText);
objWriter.WriteEndElement();//RoomTypeCode
objWriter.WriteStartElement("Quantity");
objWriter.WriteString(nodeRoom ["Quantity"].InnerText);
objWriter.WriteEndElement();//数量
objWriter.WriteStartElement("NumberOfAdults");
objWriter.WriteString(nodeRoom ["NumberOfAdults"].InnerText);
objWriter.WriteEndElement();//成人数
objWriter.WriteStartElement("NumberOfChildren");
objWriter.WriteString(nodeRoom ["NumberOfChildren"].InnerText);
objWriter.WriteEndElement();//Numberofchildren
objWriter.WriteStartElement("PricePerRoom");
objWriter.WriteString(nodeRoom ["PricePerRoom"].InnerText);
objWriter.WriteEndElement();//PricePerRoom
objWriter.WriteStartElement("RateTypes");
XmlNodeList nodeRatePlans = nodeRoom.SelectNodes("Room/RateTypes/RateType",ns);
foreach(nodeRatePlans中的XmlNode nodeRatePlan)
{
objWriter.WriteStartElement("RateType");
objWriter.WriteStartElement("Date");
objWriter.WriteString(CheckIn);
objWriter.WriteEndElement();//日期
objWriter.WriteStartElement("Price");
//将价格放在这里
objWriter.WriteEndElement();//价格
objWriter.WriteStartElement("RateTypeCode");
//将RateTypeCode放在此处
objWriter.WriteEndElement();//RateTypeCode
objWriter.WriteStartElement("BoardTypeCode");
//此处为PutBoardTypeCode
objWriter.WriteEndElement();//BoardTypeCode
objWriter.WriteEndElement();//RateType
}
objWriter.WriteEndElement();//RateTypes
objWriter.WriteEndElement();//Room
}
objWriter.WriteEndElement();//房间
费率计划是第二个节点列表.
请帮忙!
谢谢
James
I''m looping through an XML document that has two lists, so for example:
<xml>
<nodelist1 A>
<node1></node1>
<node2></node2>
<nodelist2 A>
<node3>
<node4>
</nodelist2>
</nodelist1>
<nodelist1 B>
<node1></node1>
<node2></node2>
<nodelist2 B>
<node3>
<node4>
</nodelist2>
</nodelist1>
</xml>
Looping through the first node list is fine, but I can''t get the second one to work.
Here is a code sample:
nodeRooms = xmlDoc.SelectNodes("/test:Reservation/test:Rooms/test:Room", ns);
foreach (XmlNode nodeRoom in nodeRooms)
{
objWriter.WriteStartElement("Room");
objWriter.WriteStartElement("RoomTypeCode");
objWriter.WriteString(nodeRoom["RoomTypeCode"].InnerText);
objWriter.WriteEndElement();//RoomTypeCode
objWriter.WriteStartElement("Quantity");
objWriter.WriteString(nodeRoom["Quantity"].InnerText);
objWriter.WriteEndElement();//Quantity
objWriter.WriteStartElement("NumberOfAdults");
objWriter.WriteString(nodeRoom["NumberOfAdults"].InnerText);
objWriter.WriteEndElement();//Numberofadults
objWriter.WriteStartElement("NumberOfChildren");
objWriter.WriteString(nodeRoom["NumberOfChildren"].InnerText);
objWriter.WriteEndElement();//Numberofchildren
objWriter.WriteStartElement("PricePerRoom");
objWriter.WriteString(nodeRoom["PricePerRoom"].InnerText);
objWriter.WriteEndElement();//PricePerRoom
objWriter.WriteStartElement("RateTypes");
XmlNodeList nodeRatePlans = nodeRoom.SelectNodes("Room/RateTypes/RateType", ns);
foreach (XmlNode nodeRatePlan in nodeRatePlans)
{
objWriter.WriteStartElement("RateType");
objWriter.WriteStartElement("Date");
objWriter.WriteString(CheckIn);
objWriter.WriteEndElement();//Date
objWriter.WriteStartElement("Price");
//Put Price Here
objWriter.WriteEndElement();//Price
objWriter.WriteStartElement("RateTypeCode");
//Put RateTypeCode Here
objWriter.WriteEndElement();//RateTypeCode
objWriter.WriteStartElement("BoardTypeCode");
//PutBoardTypeCode Here
objWriter.WriteEndElement();//BoardTypeCode
objWriter.WriteEndElement();//RateType
}
objWriter.WriteEndElement();//RateTypes
objWriter.WriteEndElement();//Room
}
objWriter.WriteEndElement();//Rooms
The rate plans is the second nodelist.
Please help!
Thanks
James
推荐答案
<?xml version="1.0" encoding="utf-8"?>
<Reservation xmlns="http://www.transhotel.com/transHotel/2004A"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.transhotel.com/transHotel/2004A ReservationRQ.xsd">
<WholesalerID>TRANSHOTEL</WholesalerID>
<Product>1</Product>
<ID>28610294</ID>
<CommitLevel>1</CommitLevel>
<HotelCode>1154718</HotelCode>
<CheckIn>2010-04-21</CheckIn>
<CheckOut>2010-04-22</CheckOut>
<CustomerName>availability search only</CustomerName>
<Remarks />
<TotalPrice>300.0</TotalPrice>
<Currency>GBP</Currency>
<Rooms>
<Room>
<RoomTypeCode>SDBL</RoomTypeCode>
<Quantity>1</Quantity>
<NumberOfAdults>2</NumberOfAdults>
<NumberOfChildren>0</NumberOfChildren>
<PricePerRoom>120.0</PricePerRoom>
<PricePerBoard>0.0</PricePerBoard>
<RateTypes>
<RateType>
<RateTypeCode>XX</RateTypeCode>
<BoardTypeCode>DE</BoardTypeCode>
<BoardPrice>0.0</BoardPrice>
<Price>120.0</Price>
<Date>2010-04-21</Date>
</RateType>
</RateTypes>
</Room>
<Room>
<RoomTypeCode>STTP</RoomTypeCode>
<Quantity>1</Quantity>
<NumberOfAdults>3</NumberOfAdults>
<NumberOfChildren>0</NumberOfChildren>
<PricePerRoom>180.0</PricePerRoom>
<PricePerBoard>0.0</PricePerBoard>
<RateTypes>
<RateType>
<RateTypeCode>XX</RateTypeCode>
<BoardTypeCode>DE</BoardTypeCode>
<BoardPrice>0.0</BoardPrice>
<Price>180.0</Price>
<Date>2010-04-21</Date>
</RateType>
</RateTypes>
</Room>
</Rooms>
</Reservation>
这篇关于遍历子列表-XMLNodeList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!