请检查我的XPath查询

时间:2009-12-29 12:29:04

标签: asp.net xml xpath

请告诉我哪里有问题。我写了这个xpath查询,但它没有回复我的任何节点。 我想从中选择“HotelName”: -

我的选择代码:

 Dim xmlPath As String = Server.MapPath("aa.xml")
 Dim doc As XmlDocument = New XmlDocument()
 doc.Load(xmlPath)
 Dim nodeList As XmlNodeList = doc.DocumentElement.SelectNodes("//HotelInfo/HotelName")
 For Each child As XmlNode In nodeList
     Response.Write("Node Name: " + child.Name)
     Response.Write("Node Value:" + child.FirstChild.Value)
 Next

我的xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<OTA_HotelDescriptiveInfoRS xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 OTA_HotelDescriptiveInfoRS.xsd" TimeStamp="2009-12-29T06:41:55-05:00" Version="1.006" PrimaryLangID="it" EchoToken="1" Target="Test">
  <Success />
  <Warnings>
    <Warning Code="999" Type="2"> Your request's version is earlier than our supported version. We tried to process your request in case the versions are compatible. We support version 1.006 for the OTA_HotelDescriptiveInfoRQ call.</Warning>
  </Warnings>
  <HotelDescriptiveContents HotelCode="112" HotelCodeContext="HCL" HotelName="Hostal Cruz Sol" HotelCityCode="335">
    <HotelDescriptiveContent HotelCode="112" HotelCodeContext="HCL" HotelName="Hostal Cruz Sol" HotelCityCode="335" CurrencyCode="EUR">
      <HotelInfo>
        <HotelName>Hostal Cruz Sol</HotelName>

1 个答案:

答案 0 :(得分:3)

试试这个:

Dim nsmgr as  XmlNamespaceManager = new XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ota", "http://www.opentravel.org/OTA/2003/05")

Dim nodeList As XmlNodeList = 
    doc.DocumentElement.SelectNodes("//ota:HotelInfo/ota:HotelName", nsmgr)

xmlns添加了默认命名空间,您需要在XPath表达式中处理它。