简体   繁体   English

在vb.net中添加XML名称空间引用

[英]Adding XML namespace reference in vb.net

I'm writing a piece of software that accepts XML from our clients. 我正在写一个可以接受来自客户的XML的软件。 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 xml有2个部分,一个标准部分(包含set字段)和一个自由格式部分,允许我们的客户添加自己的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. 设置系统的方式是,TotalDocument的架构涵盖了XML的所有部分,除了FreeXML标记内的内容。 The contents of the FreeXML tags has it's own schema sent to us by our client. FreeXML标记的内容由我们的客户发送给我们自己的架构。

<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 在这种情况下,客户端的xml如下所示

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

The program is trying to extract the client's custom xml for further processing. 该程序正在尝试提取客户端的自定义xml进行进一步处理。

So far, no problems. 到目前为止,没有问题。 this all reads nicely into an xmldocument. 这一切都很好地读入了xmldocument。 Unfortunatly some of our clients use namespace prefixes on their custom xml without declaring the prefixes in the xml document. 不幸的是,我们的某些客户端在其自定义xml上使用名称空间前缀,而未在xml文档中声明前缀。

<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. 由于未在xml中声明前缀,因此这会导致xmldocument掉线。 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. 每个XML文件可以在不同部分中包含多个FreeXML元素(因此,不可能提取FreeXML部分,因为不同的客户端使用1个或多个并且在整个文档的不同位置使用部分。
  • We cannot edit the clients' schema. 我们无法编辑客户的架构。
  • We cannot tell the clients to sort their act out and write working xml. 我们无法告诉客户整理其行为并编写有效的xml。

Ideally it would be best if we can just specify the namespace and prefix to the xmldocument reader. 理想情况下,最好只指定xmldocument阅读器的名称空间和前缀。 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. 解决此问题的方法似乎是更改xml加载到xml文档中的方式。 Whereas before i was parsing a string into the loadxml method of the xmldocument. 而在我之前将字符串解析为xmldocument的loadxml方法中。 I now parse a string into a stringreader, then parse the stringreader into an xmltextreader. 我现在将字符串解析为stringreader,然后将stringreader解析为xmltextreader。 The xmltextreader has the Namespaces property which allows you to turn namespace validataion off. xmltextreader具有Namespaces属性,该属性使您可以关闭名称空间验证。 The xmltextreader can then be parsed into the load method of the xmldocument. 然后可以将xmltextreader解析为xmldocument的load方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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