Can't load XML data

L

Lou

I have a app that gets xml data from a web site using http:
This all works if I test it in VB6, but the ParseXMLFile function
crashes on the line:
nNode = xDoc.SelectSingleNode("cap:alert")
With the error message
"Namespace Manager or XsltContext needed. This query has a prefix, variable,
or user-defined function."

Why does this work in VB6 but not VB .Net?

..Net code

' This causes the round-trip

Dim rsp As WebResponse = req.GetResponse()

sr = New StreamReader(WebResp.GetResponseStream(), True)

myString = sr.ReadToEnd()

ParseXMLFile(myString)



Private Sub ParseXMLFile(ByVal sXML As String)

Dim xDoc As New XmlDocument()

Dim nNode As XmlNode

Dim nNodes As XmlNodeList

xDoc.LoadXml(sXML)

nNode = xDoc.SelectSingleNode("cap:alert")

If Not nNode Is Nothing Then

nNodes = nNode.SelectNodes("cap:info")

If Not nNodes Is Nothing Then

Debug.Print(nNodes.Count.ToString)

End If

End If

End Sub



'VB6 code that works perfectly

Dim nNode As IXMLDOMNode
Dim nNodes As IXMLDOMNodeList

objhttp.Open "GET", "http://www.weather.gov/alerts/al.cap", False
objhttp.send

'this puts the result directly into an xml domdocument
Set xmlDocument = objhttp.responseXML
Debug.Print xmlDocument.xml
Set nNode = xmlDocument.selectSingleNode("cap:alert")
Set nNodes = nNode.selectNodes("cap:info")
 
P

Patrick Steele

I have a app that gets xml data from a web site using http:
This all works if I test it in VB6, but the ParseXMLFile function
crashes on the line:
nNode = xDoc.SelectSingleNode("cap:alert")
With the error message
"Namespace Manager or XsltContext needed. This query has a prefix, variable,
or user-defined function."

You need to add the defined "cap" namespace to the XmlDocument object.
Something like this should work:

xDoc.NameTable.Add("cap")
Dim ns As XmlNamespaceManager = new XmlNamespaceManager(xDoc.NameTable)
ns.AddNamespace("cap", "http://www.incident.com/cap/1.0")
 
L

Lou

Thank Pat, I just tried that, now I get the same error:
{"Namespace Manager or XsltContext needed. This query has a prefix,
variable, or user-defined function."}

Here is my code
Dim sXML as string="http://www.weather.gov/alerts/al.cap"
Dim xDoc As New XmlDocument()

Dim nNode As XmlNode

Dim nNodes As XmlNodeList

xDoc.NameTable.Add("cap")

Dim ns As XmlNamespaceManager = New XmlNamespaceManager(xDoc.NameTable)

ns.AddNamespace("cap", "http://www.incident.com/cap/1.0")



xDoc.LoadXml(sXML)



'THIS IS THE :INE THAT CRASHES

nNode = xDoc.SelectSingleNode("cap:alert")

If Not nNode Is Nothing Then

nNodes = nNode.SelectNodes("cap:info")

If Not nNodes Is Nothing Then

Debug.Print(nNodes.Count.ToString)

End If

End If
 
P

Patrick Steele

'THIS IS THE :INE THAT CRASHES

nNode = xDoc.SelectSingleNode("cap:alert")

Doh!! Sorry. Now that's you've got that namespace manager, you need to
pass it to your queries:

nNode = xDoc.SelectSingleNode("cap:alert", ns)

Have fun with that stuff. I wrote a compact framework app that queried
that data from my phone so I can check out any severe weather alerts in
the area. :)
 
L

Lou

Thanks. That worked great!

Patrick Steele said:
Doh!! Sorry. Now that's you've got that namespace manager, you need to
pass it to your queries:

nNode = xDoc.SelectSingleNode("cap:alert", ns)

Have fun with that stuff. I wrote a compact framework app that queried
that data from my phone so I can check out any severe weather alerts in
the area. :)
 
Top