简体   繁体   中英

Adding XML namespace reference in vb.net

I'm writing a piece of software that accepts XML from our clients. The xml has2 parts, a standard part that contains set fields, and a freeform part that allows our clients to add own their own xml

<OverallDocument>
    <SetFields>
        <name>Jon Doe</name>
        <age>24</age>
        <sex>M</sex>
    </SetFields>
    <FreeXML>
    <!--custom xml goes here-->
    </FreeXML>
</OverallDocument>

The system is set so that the OverallDocument has a schema that covers all sections of the xml except what goes inside the FreeXML tags. The contents of the FreeXML tags has it's own schema sent to us by our client.

<OverallDocument>
    <SetFields>
        <name>Jane Doe</name>
        <age>30</age>
        <sex>F</sex>
    </SetFields>
    <FreeXML>
    <Custom1>
        <CustomString>aaaaaa</CustomString>
        <CustomInt>12345</CustomInt>
    </Custom1>
    </FreeXML>
</OverallDocument>

In this case the client's xml looks like this

<Custom1>
    <CustomString>aaaaaa</CustomString>
    <CustomInt>12345</CustomInt>
</Custom1>

The program is trying to extract the client's custom xml for further processing.

So far, no problems. this all reads nicely into an xmldocument. Unfortunatly some of our clients use namespace prefixes on their custom xml without declaring the prefixes in the xml document.

<OverallDocument>
    <SetFields>
        <name>Jane Doe</name>
        <age>30</age>
        <sex>F</sex>
    </SetFields>
    <FreeXML>
    <hl:Custom1>
        <CustomString>aaaaaa</CustomString>
        <CustomInt>12345</CustomInt>
    </hl:Custom1>
    </FreeXML>
</OverallDocument>

This causes the xmldocument to fall over as the prefixes are not declared in the xml. I tried getting around this by removing all namespace prefixes from the code, but this causes issues later on in the processing as the clients' schemas require the prefixes to be on the tags.

some further problems

  • We have many clients with different schemas and different namespaces.
  • Each XML file can have multiple FreeXML elements in different sections (so it's not possible to simply extract the FreeXML section as different clients use 1 or more and use sections in different locations throughout the document.
  • We cannot edit the clients' schema.
  • We cannot tell the clients to sort their act out and write working xml.

Ideally it would be best if we can just specify the namespace and prefix to the xmldocument reader. eg

dim xdoc as xmldocument = xmldocument
'add namespace and prefix
xdoc.loadxml(xmlcode)

It seems the way to fix this is to change the way the xml is loaded into the xmldocument. Whereas before i was parsing a string into the loadxml method of the xmldocument. I now parse a string into a stringreader, then parse the stringreader into an xmltextreader. The xmltextreader has the Namespaces property which allows you to turn namespace validataion off. The xmltextreader can then be parsed into the load method of the xmldocument.

Dim xstring As String = xmldata
Dim sreader As New System.IO.StringReader(xstring) 'load string into stringreader
Dim xreader As New XmlTextReader(sreader)          'load stringreader into xmltextreader
xreader.Namespaces = False                         'turn off namespaces
Dim xdoc As XmlDocument = New XmlDocument          'create xmldocument
xdoc.Load(xreader)                                 'Load xmltextreader into xmldocument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM