Solved

Reading nodes in XMLDocument when xmlns is specified

Posted on 2013-01-13
13
316 Views
Last Modified: 2013-02-04
I am using System.Xml.XmlDocument to read a XML file.

Example:

<?xml version="1.0" encoding="utf-8"?>
<response xmlns="http://www.hostserver.com/api" status="ok">
  <client>
    <client_id>5</client>
    <name>John Smith</name>
  </client>
</response>

I can load the XML into XMLDocument without issue but I am unable to use SelectSingleNode to extract any details, it is blank.

Dim objXml As New System.Xml.XmlDocument

objXml.LoadXml(strXML)

MsgBox(objXml.SelectSingleNode("//response/client/client_id"))

I believe it is due to the xmlns namespace.  Is there a way to ignore, remove or elegantly move around it?
0
Comment
Question by:Officedoxs
  • 6
  • 6
13 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38773472
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 300 total points
ID: 38773652
refer to the official doc, and include a name space manager:
http://msdn.microsoft.com/en-us/library/h0hw012b.aspx
 XmlNamespaceManager nsmgr = new XmlNamespaceManager(objXml.NameTable);
 nsmgr.AddNamespace("ab", "http://www.hostserver.com/api");
 XmlNode book = objXml.SelectSingleNode("//ab:client/clientid", nsmgr); 

Open in new window

0
 

Author Comment

by:Officedoxs
ID: 38773905
Looks like I am able to grab the node, but I'm a little unclear how to grab the value out of the client_id field; the "ab" appears to replace the "response" group.

I tried book.InnerText and book.Value and recieve a NullReferenceException.

I did notice it was "client_id" rather than "clientid".  Fixed.

I'm using the live API which has some additional values so perhaps I will test with a self-crated XML file to ensure it works unless you can see something out of place.

Thank you.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38773934
>but I'm a little unclear how to grab the value out of the client_id field
book  XmlNode has the properties of Value that should do...
0
 

Author Comment

by:Officedoxs
ID: 38775140
Strange.  I'll verify the XML again as I have tried the .Value field.  Seemed logical obviously.
0
 

Author Comment

by:Officedoxs
ID: 38781158
I'm unfortunately still unable to resolve the issue.

I have a block of code which should demonstrate the problem clearly.

It returns with NullReferenceException on the "book.Value" line.

 
      Dim strXML As String = _
      "<?xml version=""1.0"" encoding=""utf-8""?>" & vbCrLf & _
      "<response xmlns=""http://www.hostserver.com/api"" status=""ok"">" & vbCrLf & _
      "  <client>" & vbCrLf & _
      "    <client_id>5</client_id>" & vbCrLf & _
      "    <name>John Smith</name>" & vbCrLf & _
      "  </client>" & vbCrLf & _
      "</response>" & vbCrLf

      Dim xmlReader As New System.Xml.XmlDocument

      xmlReader.LoadXml(strXML)

      Dim nsmgr As New System.Xml.XmlNamespaceManager(xmlReader.NameTable)
      nsmgr.AddNamespace("ab", "http://www.hostserver.com/api")

      Dim book As System.Xml.XmlNode

      book = xmlReader.SelectSingleNode("//ab:client/client_id", nsmgr)

      MsgBox(book.Value)

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 300 total points
ID: 38781630
this will do:
      book = xmlReader.SelectSingleNode("//ab:client_id", nsmgr) 

Open in new window

0
 

Author Comment

by:Officedoxs
ID: 38785854
While I don't a NullReferenceException or any other error, it simply returns an empty/blank value.  Any other ideas?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38785971
interesting, as I tested the code. though with C#, but otherwise the same..
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38785973
can you inspect the book variable after that line "book = ..." to see which property/method would return the "5"?
0
 

Accepted Solution

by:
Officedoxs earned 0 total points
ID: 38817322
Success, I was able to return the value using .InnerText.

Is there any ways to specify the full pathing, something along these lines?

book = xmlReader.SelectSingleNode("//ab:response/client/client_id", nsmgr)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38822412
I tried some ways, but with namespaces, I failed to get the correct result.
anyhow, I am not a true XML experts, just "dangerous" enough :)
0
 

Author Closing Comment

by:Officedoxs
ID: 38850507
I was able to get full pathing:

      Dim strXML As String = _
      "<?xml version=""1.0"" encoding=""utf-8""?>" & vbCrLf & _
      "<response xmlns=""http://www.hostserver.com/api"" status=""ok"">" & vbCrLf & _
      "  <client>" & vbCrLf & _
      "    <client_id>5</client_id>" & vbCrLf & _
      "    <name>John Smith</name>" & vbCrLf & _
      "  </client>" & vbCrLf & _
      "</response>" & vbCrLf

      Dim xmlReader As New System.Xml.XmlDocument

      xmlReader.LoadXml(strXML)

      Dim objNSManager As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(xmlReader.NameTable)
      objNSManager.AddNamespace("api", "http://www.hostserver.com/api")

      MsgBox(xmlReader.SelectSingleNode("//api:response/api:client/api:client_id", objNSManager).InnerText)
      MsgBox(xmlReader.SelectSingleNode("//api:response/api:client/api:name", objNSManager).InnerText)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 3 38
How do I get the id from URL? 19 48
Wpf develop 5 32
Installing IIS after Visual Studio 2015 Community Edition 5 17
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now