Solved

Reading nodes in XMLDocument when xmlns is specified

Posted on 2013-01-13
13
319 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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