Houlahan
Houlahan

Reputation: 783

creating xml file with namespaces

I'm trying to create a xml file:

    <?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">

I have the following code:

    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"))

however when i run the code i get the following error:

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

Edit

ok so i got the namespaces to work with the following code:

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"))), _

but now in my xml i have:

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

is there any way of getting rid of the blank xmlns="" within the orderHead tag?

Upvotes: 1

Views: 1198

Answers (1)

John Saunders
John Saunders

Reputation: 161773

What do you get from this:

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"))

Sorry, but I don't have time to test it right now.

Upvotes: 2

Related Questions