tags:

views:

100

answers:

1

please tell me where is problem. i wrote this xpath query but its not retriveing me any node. i want to select "HotelName" from it:-

my selector code:

 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

my xml is like this:

<?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>
+3  A: 

Try this:

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)

That xmlns added a default namespace, and you need to deal with it in your XPath expression.

Rubens Farias
+1: Better way of writing my answer (since deleted). It's worth mentioning this very thorough explanation IMHO: http://msdn.microsoft.com/en-us/library/ms950779.aspx
Rob Fonseca-Ensor
wow its working thanks lot. you guys are great techi. please give me any link to know more about this matter.
Rajesh Rolen- DotNet Developer
Rob link seems very complete
Rubens Farias