VS 2010 Xpath query on an XML returns 0 results-VBForums
Results 1 to 7 of 7

Thread: Xpath query on an XML returns 0 results

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Question Xpath query on an XML returns 0 results

    Hi guys !

    A little something i hope you could help me with -

    I've converted an html to XML and now i want to get all the child nodes nested
    within a DIV element with a specific attribute (class="itemInfo").

    Because the html that the XML is based on changes from time to time i want to use a more "safe" relative root,
    (something like : "//div[@class='itemInfo']").

    Problem is that i always get 0 nodes ...
    I'm sure that something is wrong with my Xpath syntax, just don't know what it is.

    Here is my code, please, any help would be appreciated.

    Code:
            Dim doc As New System.Xml.XmlDocument()
            doc.Load("products.xml")
            
            Dim root As System.Xml.XmlElement = doc.DocumentElement
            
            Dim nodes As System.Xml.XmlNodeList = root.SelectNodes("//descendant::div[@class='itemInfo']")
            
            Console.WriteLine(nodes.Count) '<-- always get 0
            
            For Each node As System.Xml.XmlNode In nodes
                Dim name As String = node("name").InnerText
                Dim title As String = node("title").InnerText
                Console.Write("name" & vbTab & "{0}" & vbTab & "title" & vbTab & "{1}", name, title)
            Next
    I also tried
    Code:
    root.SelectNodes("//div[@class='itemInfo']")
    and
    Code:
    root.SelectNodes("/*//div[@class='itemInfo']")
    but nothing seems to work.

    Thanks in advance !

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: Xpath query on an XML returns 0 results

    The problem seems to be with the <html> tag attribute :
    <html xmlns="http://www.w3.org/1999/xhtml">

    As soon as i removed the xmlns attribute my "selectNodes" method started working ...

    So now -
    Is there a way to still make it working without removing that xmlns attribute ?

    Thanks !

  3. #3
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    19,090

    Re: Xpath query on an XML returns 0 results

    does it have to be xPath?
    html is an XML Namespace. search for that + you should find an answer

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: Xpath query on an XML returns 0 results

    So i've searched the net and found something that looks like a solution,
    i've changed my code to :

    Code:
            Dim doc As New System.Xml.XmlDocument()
            doc.Load("products.xml")
            
            Dim root As System.Xml.XmlElement = doc.DocumentElement
            
            Dim nsMgr As XmlNamespaceManager = New XmlNamespaceManager(root.OwnerDocument.NameTable)
            nsMgr.AddNamespace("http://www.w3.org/1999/xhtml", "ns")
            nodeList = root.SelectNodes("descendant::ns:div[@class='itemInfo']", nsMgr)
            
            Console.WriteLine(nodes.Count) 
            
            For Each node As System.Xml.XmlNode In nodes
                Dim name As String = node("name").InnerText
                Dim title As String = node("title").InnerText
                Console.Write("name" & vbTab & "{0}" & vbTab & "title" & vbTab & "{1}", name, title)
            Next
    But now i'm getting an error :"Namespace prefix 'ns' is not defined."



    Quote Originally Posted by .paul. View Post
    does it have to be xPath?
    html is an XML Namespace. search for that + you should find an answer

  5. #5
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    19,090

    Re: Xpath query on an XML returns 0 results

    if you upload your xml (or an example xml with the same layout) i'll debug it for you

  6. #6
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    19,090

    Re: Xpath query on an XML returns 0 results

    i looked up XmlNamespaceManager + it seems you're using it wrong:

    (from: http://msdn.microsoft.com/en-us/library/d6730bwt(v=vs.80).aspx )

    vb Code:
    1. nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    20

    Re: Xpath query on an XML returns 0 results

    Works like a charm :-)
    Thanks mate !

    Quote Originally Posted by .paul. View Post
    i looked up XmlNamespaceManager + it seems you're using it wrong:

    (from: http://msdn.microsoft.com/en-us/library/d6730bwt(v=vs.80).aspx )

    vb Code:
    1. nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.