无法获取XmlDocument.SelectNodes来检索值?

时间:2014-11-09 18:42:41

标签: xml nodes

XML文档:

<HotelValuedAvailRS xmlns="http://www.hotelbeds.com/schemas/2005/06/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hotelbeds.com/schemas/2005/06/messages HotelValuedAvailRS.xsd" timeToExpiration="1799971" totalItems="90" echoToken="DummyEchoToken"><AuditData><ProcessTime>275</ProcessTime><Timestamp>2014-11-04 11:39:28.253</Timestamp><RequestHost>54.169.51.224</RequestHost><ServerName>FORM</ServerName><ServerId>FO</ServerId><SchemaRelease>2005/06</SchemaRelease><HydraCoreRelease>2014.07.10.PCI</HydraCoreRelease><HydraEnumerationsRelease>N/A</HydraEnumerationsRelease><MerlinRelease>N/A</MerlinRelease></AuditData><PaginationData currentPage="1" totalPages="5"/><ServiceHotel xsi:type="ServiceHotel" availToken="18S4Jo2aVQeZLWqDTnJ3bwbh"><ContractList><Contract><Name>ID_B2B_24#BARI</Name><IncomingOffice code="1"></IncomingOffice><Classification code="NOR">Online Price</Classification></Contract></ContractList><DateFrom date="20141228"/><DateTo date="20141230"/><Currency code="EUR">Euro</Currency><HotelInfo xsi:type="ProductHotel"><Code>271</Code><Name>TRYP Palma Bellver Hotel</Name><ImageList><Image><Type>jpg</Type><Order>1</Order><VisualizationOrder>1</VisualizationOrder><Url>http://www.hotelbeds.com/giata/small/00/000271/000271a_hb_a_003.jpg</Url></Image><Image><Type>jpg</Type><Order>2</Order><VisualizationOrder>2</VisualizationOrder><Url>http://www.hotelbeds.com/giata/small/00/000271/000271a_hb_w_001.jpg</Url></Image></ImageList><Category type="SIMPLE" code="4EST" shortname="4*">4 STARS</Category><Destination type="SIMPLE" code="PMI"><Name>Majorca</Name><ZoneList><Zone type="SIMPLE" code="10">Palma</Zone></ZoneList></Destination><ChildAge ageFrom="0" ageTo="0"/><Position latitude="39.56860999999999961574" longitude="2.63178000000000000824"/></HotelInfo><AvailableRoom><HotelOccupancy><RoomCount>1</RoomCount><Occupancy><AdultCount>2</AdultCount><ChildCount>0</ChildCount></Occupancy></HotelOccupancy><HotelRoom SHRUI="F7TSfb7tBLYH3W4X5CaNVA==" availCount="99" onRequest="N"><Board type="SIMPLE" code="SH-E10" shortname="RO">ROOM ONLY</Board><RoomType type="SIMPLE" code="DBL-E10" characteristic="ST">DOUBLE STANDARD</RoomType><Price><Amount>149.740</Amount></Price></HotelRoom></AvailableRoom><AvailableRoom><HotelOccupancy><RoomCount>1</RoomCount><Occupancy><AdultCount>2</AdultCount><ChildCount>0</ChildCount></Occupancy></HotelOccupancy><HotelRoom SHRUI="7zsBOMYAObs09Gt/WwvZIg==" availCount="99" onRequest="N"><Board type="SIMPLE" code="HD-E10" shortname="BB">BED AND BREAKFAST</Board><RoomType type="SIMPLE" code="DBL-E10" characteristic="ST">DOUBLE STANDARD</RoomType><Price><Amount>190.120</Amount></Price></HotelRoom></AvailableRoom><AvailableRoom><HotelOccupancy><RoomCount>1</RoomCount><Occupancy><AdultCount>2</AdultCount><ChildCount>0</ChildCount></Occupancy></HotelOccupancy><HotelRoom SHRUI="HMrRj2pAKzao/Y1tnmr20A==" availCount="99" onRequest="N"><Board type="SIMPLE" code="SH-E10" shortname="RO">ROOM ONLY</Board><RoomType type="SIMPLE" code="DBL-E10" characteristic="VM">DOUBLE SEA VIEW</RoomType><Price><Amount>203.580</Amount></Price></HotelRoom></AvailableRoom><AvailableRoom><HotelOccupancy><RoomCount>1</RoomCount><Occupancy><AdultCount>2</AdultCount><ChildCount>0</ChildCount></Occupancy></HotelOccupancy><HotelRoom SHRUI="1+9/i90A0ZqbOmZlDSFStw==" availCount="99" onRequest="N"><Board type="SIMPLE" code="HD-E10" shortname="BB">BED AND BREAKFAST</Board><RoomType type="SIMPLE" code="DBL-E10" characteristic="VM">DOUBLE SEA VIEW</RoomType><Price><Amount>243.960</Amount></Price></HotelRoom></AvailableRoom><AvailableRoom><HotelOccupancy><RoomCount>1</RoomCount><Occupancy><AdultCount>2</AdultCount><ChildCount>0</ChildCount></Occupancy></HotelOccupancy><HotelRoom SHRUI="sLrp9E+6lq/FBOsvMhv7Qw==" availCount="99" onRequest="N"><Board type="SIMPLE" code="SH-E10" shortname="RO">ROOM ONLY</Board><RoomType type="SIMPLE" code="JSU-E10" characteristic="ST">JUNIOR SUITE STANDARD</RoomType><Price><Amount>358.370</Amount></Price></HotelRoom></AvailableRoom><AvailableRoom><HotelOccupancy><RoomCount>1</RoomCount><Occupancy><AdultCount>2</AdultCount><ChildCount>0</ChildCount></Occupancy></HotelOccupancy><HotelRoom SHRUI="KiWIO7156n0+4HmG3UiKrw==" availCount="99" onRequest="N"><Board type="SIMPLE" code="HD-E10" shortname="BB">BED AND BREAKFAST</Board><RoomType type="SIMPLE" code="JSU-E10" characteristic="ST">JUNIOR SUITE STANDARD</RoomType><Price><Amount>398.750</Amount></Price></HotelRoom></AvailableRoom></ServiceHotel></HotelValuedAvailRS>

C#代码:

XmlDocument xdcDocument = new XmlDocument();

xdcDocument.Load(@"E:\\Hotel.xml");

XmlElement xelRoot = xdcDocument.DocumentElement;
XmlNodeList listOfContacts = xdcDocument.SelectNodes("/HotelValuedAvailRS/ServiceHotel/HotelInfo");
foreach (XmlNode xndNode in listOfContacts)
{
    string name = xndNode["Name"].InnerText;
    string order = xndNode["Order"].InnerText;
}

我没有通过上面的代码从上面的XML中获得任何细节。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您用于选择listOfContacts的xpath与文档中的内容不匹配。更改它以指定您感兴趣的节点的实际路径。

实际上,循环中的元素名称也没有。您期望文档中的哪些节点匹配?

编辑#1:实际上,现在你已经指定了整个文档,这更有意义。我必须自己编译并运行它,但事实证明你在xpath中缺少一个名称空间声明。您已将根元素声明为名称空间http://www.hotelbeds.com/schemas/2005/06/messages,但您只有xpath选择无名称空间中的元素。

解决此问题的最简单方法是在xpath中的每个元素名称前面使用前缀:

"/ns:HotelValuedAvailRS/ns:ServiceHotel/ns:HotelInfo"

并将定义了此前缀的XmlNamespaceManager传递给SelectNodes