Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading nodes in XMLDocument when xmlns is specified

Posted on 2013-01-13
13
Medium Priority
?
325 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

886 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