INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Parse XML with XmlDocument

Parse XML with XmlDocument

(OP)
I have an xml file I'm trying to read that looks like this:

CODE

<?xml version="1.0" encoding="utf-8"?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:cda="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 http://xreg2.nist.gov:8080/hitspValidation/schema/cdar2c32/infrastructure/cda/C32_CDA.xsd" xmlns:sdtc="urn:hl7-org:sdtc">
  <recordTarget>
    <patientRole>
      <addr>
         <streetAddressLine>123 MAIN ST</streetAddressLine>
      </addr>		
      <addr>
         <streetAddressLine>987 NORTH ST</streetAddressLine>
      </addr>				
    </patientRole>
  </recordTarget>
</ClinicalDocument> 

And I'm trying to read and output all the streetAddressLine. My code is

CODE

Dim xmlDoc As New XmlDocument()
Dim namespaces As New XmlNamespaceManager(xmlDoc.NameTable)
namespaces.AddNamespace("ns", "urn:hl7-org:v3")

xmlDoc.Load("C:\testXML.xml")

Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/ClinicalDocument/recordTarget/patientRole/addr", namespaces)
Dim addr As String = ""
For Each node As XmlNode In nodes
  addr = node.SelectSingleNode("streetAddressLine").InnerText
  MessageBox.Show(addr)
Next 

It will not output the addressStreetLine. If I remove the xmlns="urn:hl7-org:v3" namespace it works fine. What do I need to do to get it to work with the namespace.

Thanks,

RE: Parse XML with XmlDocument

try the following

CODE --> VB

Dim xmlDoc As New XmlDocument()
Dim namespaces As New XmlNamespaceManager(xmlDoc.NameTable)
namespaces.AddNamespace("ns", "urn:hl7-org:v3")

xmlDoc.Load("C:\testXML.xml")

Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/ns:ClinicalDocument/ns:recordTarget/ns:patientRole/ns:addr", namespaces)
Dim addr As String = ""
For Each node As XmlNode In nodes
  addr = node.SelectSingleNode("ns:streetAddressLine", namespaces).InnerText
  MessageBox.Show(addr)
Next 

RE: Parse XML with XmlDocument

Note that what you are asking isn't parsing XML at all, the XmlDocument is doing all of the parsing when you load it.

What you are asking about is spelunking the DOM, which can be done by iterating and descending the DOM tree or when (rarely) warranted the slower approach relying on XPath queries as given above.

You should be able to get and iterate the <addr/> node collection far more simply and directly.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close