1

我正在尝试创建一个 xml 文件:

    <?xml version="1.0"?>
<Order xmlns="urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:schemas-basdaorg:2000:purchaseOrder:xdr:3.01order-v3.xsd">

我有以下代码:

    Dim xsi As XNamespace = "http://www.w3.org/2001/XMLSchema-instance"
    Dim schemaLocation As XNamespace = "urn:schemas-basdaorg:2000:purchaseOrder:xdr:3.01order-v3.xsd"
    Dim order As XNamespace = "urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01"

    Dim root As New XElement("Root", New XAttribute("Order", order.NamespaceName), _
                             New XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName), _
                             New XAttribute(xsi.NamespaceName + "schemaLocation", schemaLocation.NamespaceName), _
                             New XElement("Child", _
                                          New XElement("DifferentChild", "other content")), _
                             New XElement("Child2", "c2 content"), _
                             New XElement("Child3", "c3 content"))

但是,当我运行代码时,出现以下错误:

xmlexception was unhanded
The ':' character, hexadecimal value 0x3A, cannot be included in a name.

编辑

好的,所以我让命名空间可以使用以下代码:

Dim xsi As XNamespace = "http://www.w3.org/2001/XMLSchema-instance"
        Dim schemaLocation As XNamespace = "urn:schemas-basdaorg:2000:purchaseOrder:xdr:3.01order-v3.xsd"
        Dim order As XNamespace = "urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01"

        Dim root As New XElement(order + "Order", _
                                 New XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                                 New XAttribute(XNamespace.Xmlns + "schemaLocation", schemaLocation),
                                 New XElement("OrderHead", _
                                              New XElement("Schema", New XElement("Version", "3.05")),
                                              New XElement("Parameters", New XElement("Language", "en-GB"), New XElement("DecimalSeparator", "."), New XElement("Precision", "20.3")), _
                                              New XElement("OrderType", "Purchase Order", New XAttribute("Code", "PUO"))), _

但现在在我的xml中我有:

<OrderHead xmlns="">
    <Schema>
      <Version>3.05</Version>
    </Schema>
    <Parameters>

有没有办法摆脱 orderHead 标签中的空白 xmlns="" ?

4

1 回答 1

2

你从中得到什么:

Dim order As XNamespace = "urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01"

Dim root As New XElement(
    order + "Order", 
    New XElement(order + "Child", _
                 New XElement(order + "DifferentChild", "other content")), _
    New XElement(order + "Child2", "c2 content"), _
    New XElement(order + "Child3", "c3 content"))

抱歉,我现在没有时间测试它。

于 2013-02-06T00:24:29.013 回答