1

I am currently attempting to parse data with VB.net to populate some textboxes selecting by childname "eResponse.01", 02, 03 etc however a namespace / schema location in the main tag seems to be tripping up the code.

    Dim xmlDoc As New XmlDocument()
    xmlDoc.Load("C:\Users\james\Desktop\NEMSIS\EMS\xml\Test.xml")
    Dim xmlns As New XmlNamespaceManager(xmlDoc.NameTable)
    xmlns.AddNamespace("xsi", "http://www1w3.org/2001/XMLSchema-instance")
    xmlns.AddNamespace("schemaLocation", "http://www.nemsis.org http://nemsis.org/media/nemsis_v3/release-3.4.0/XSDs/NEMSIS_XSDs/EMSDataSet_v3.xsd")
    xmlns.AddNamespace("xmlns", "http://www.nemsis.org")
    Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/EMSDataSet/Header/PatientCareReport/eResponse")
    For Each node As XmlNode In nodes
        TextEdit1.Text = node.SelectSingleNode("eResponse.03").InnerText
    Next

works fine when using the following

<EMSDataSet>
<Header>
    <DemographicGroup>
        <dAgency.01>0</dAgency.01>
        <dAgency.02>00</dAgency.02>
        <dAgency.04>49</dAgency.04></DemographicGroup>
    <PatientCareReport>
        <eRecord>
            <eRecord.01>OpP</eRecord.01>
            <eRecord.SoftwareApplicationGroup>
                <eRecord.02>G</eRecord.02>
                <eRecord.03>Q</eRecord.03>
                <eRecord.04>P</eRecord.04></eRecord.SoftwareApplicationGroup></eRecord>
        <eResponse>
            <eResponse.AgencyGroup>
                <eResponse.01>a</eResponse.01>
                <eResponse.02>BL</eResponse.02></eResponse.AgencyGroup>
            <eResponse.03>u33</eResponse.03>

however it does not populate anything if I include the namespace/schema

<EMSDataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nemsis.org http://nemsis.org/media/nemsis_v3/release-3.4.0/XSDs/NEMSIS_XSDs/EMSDataSet_v3.xsd" xmlns="http://www.nemsis.org">
<Header>
    <DemographicGroup>
        <dAgency.01>0</dAgency.01>
        <dAgency.02>00</dAgency.02>
        <dAgency.04>49</dAgency.04></DemographicGroup>
    <PatientCareReport>
        <eRecord>
            <eRecord.01>OpP</eRecord.01>
            <eRecord.SoftwareApplicationGroup>
                <eRecord.02>G</eRecord.02>
                <eRecord.03>Q</eRecord.03>
                <eRecord.04>P</eRecord.04></eRecord.SoftwareApplicationGroup></eRecord>
        <eResponse>
            <eResponse.AgencyGroup>
                <eResponse.01>a</eResponse.01>
                <eResponse.02>BL</eResponse.02></eResponse.AgencyGroup>
            <eResponse.03>u33</eResponse.03>

what do I need to do to get my code to ignore extra data in the opening tag - removing that information is not an option.

3
  • possible duplicate of add namespace using xmlnamespacemanager in C# Commented Jun 24, 2015 at 19:10
  • 1
    You need to use the XmlNamespaceManager - an example is linked in my duplicate comment. Commented Jun 24, 2015 at 19:11
  • I appologize I copied old code - I have already tried namespacemanager and either failed the syntax or dont understand it - edited to show what was used Commented Jun 24, 2015 at 19:17

1 Answer 1

1

Your XML has unprefixed namespace -also known as default namespace- here :

xmlns="http://www.nemsis.org"

unlike prefixed namespace, descendant elements inherit ancestor default namespace implicitly.

To access elements in namespace, you need to use registered prefix properly in your XPath and pass the namespace manager as 2nd argument of SelectNodes() and SelectSingleNode() :

......
xmlns.AddNamespace("d", "http://www.nemsis.org")
Dim xpath As String = "/d:EMSDataSet/d:Header/d:PatientCareReport/d:eResponse"
Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes(xpath, xmlns)
For Each node As XmlNode In nodes
    TextEdit1.Text = node.SelectSingleNode("d:eResponse.03", xmlns).InnerText
Next
Sign up to request clarification or add additional context in comments.

2 Comments

I had attempted this earlier however received an error stating System.ArgumentException: Prefix "xmlns" is reserved for use by XML. at System.Xml.XmlNamespaceManager.AddNamespace(String prefix, String uri) at EPCR.Form1.Form1_Load(Object sender, EventArgs e) in F:\FTDEPCR\EPCR\EPCR\Form1.vb:line 47
I didn't aware that xmlns is reserved, thought any prefix is fine as long as it points to the correct uri

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.