Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading nodes in XMLDocument when xmlns is specified

Posted on 2013-01-13
13
Medium Priority
?
324 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
[X]
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
  • 6
  • 6
13 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1200 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
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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