在 vb.net 中添加 XML 命名空间引用 [英] Adding XML namespace reference in vb.net

查看:60
本文介绍了在 vb.net 中添加 XML 命名空间引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个接受来自我们客户的 XML 的软件.xml 有 2 个部分,一个包含设置字段的标准部分,以及一个允许我们的客户添加自己的 xml 的自由格式部分

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>

系统设置为使OverallDocument 的架构涵盖xml 的所有部分,除了FreeXML 标签内的部分.FreeXML 标签的内容有它自己的架构,由我们的客户发送给我们.

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>

在这种情况下,客户端的 xml 看起来像这样

In this case the client's xml looks like this

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

该程序正在尝试提取客户端的自定义 xml 以进行进一步处理.

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

到目前为止,没有问题.这一切都很好地读入了一个 xmldocument.不幸的是,我们的一些客户在他们的自定义 xml 中使用了命名空间前缀,而没有在 xml 文档中声明这些前缀.

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>

这会导致 xmldocument 失败,因为前缀没有在 xml 中声明.我尝试通过从代码中删除所有名称空间前缀来解决这个问题,但这会在处理过程中导致问题,因为客户端的架构要求前缀位于标签上.

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.

一些其他问题

  • 我们有许多不同的客户架构和不同的命名空间.
  • 每个 XML 文件可以有多个不同的 FreeXML 元素部分(所以不可能只需将 FreeXML 部分提取为不同的客户使用 1 个或多个使用不同位置的部分贯穿整个文档.
  • 我们无法编辑客户的架构.
  • 我们不能告诉客户排序他们的行为并编写工作 xml.

理想情况下,最好是为 xmldocument 阅读器指定命名空间和前缀.例如

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)

推荐答案

看来解决这个问题的方法是改变 xml 加载到 xmldocument 的方式.而在我将字符串解析为 xmldocument 的 loadxml 方法之前.我现在将字符串解析为 stringreader,然后将 stringreader 解析为 xmltextreader.xmltextreader 具有 Namespaces 属性,它允许您关闭命名空间验证.然后可以将 xmltextreader 解析为 xmldocument 的 load 方法.

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

这篇关于在 vb.net 中添加 XML 命名空间引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆