Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Reading nodes in XMLDocument when xmlns is specified

Posted on 2013-01-13
13
320 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 143

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 143

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 143

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 143

Expert Comment

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

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 143

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

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.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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