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.

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 :

root.SelectNodes("//div[@class='itemInfo']")

and :

root.SelectNodes("/*//div[@class='itemInfo']")

but nothing seems to work.

Thanks in advance !

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 !

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.

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 :

root.SelectNodes("//div[@class='itemInfo']")

and :

root.SelectNodes("/*//div[@class='itemInfo']")

but nothing seems to work.

Thanks in advance !

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

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."


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 !

I'm getting the strangest feeling ... like maybe talking to myself ...

Anyhow - for someone else that might use this post, her'e the solution :

Should be :
nsMgr.AddNamespace("ns", "http://www.w3.org/1999/xhtml")

Instead of :
nsMgr.AddNamespace("http://www.w3.org/1999/xhtml", "ns")

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

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."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.