使用命名空间创建xml文件

时间:2013-02-05 19:44:01

标签: .net xml vb.net xml-namespaces

我正在尝试创建一个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 =“”?

1 个答案:

答案 0 :(得分: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"))

很抱歉,我现在没有时间对它进行测试。