Rasmus Eskesen
Rasmus Eskesen

Reputation: 246

Forcing XmlWriter to use 'invalid' characters

I am converting an EDIFACT file to an OUIUBL file (technically just an XML file), (Essentially I'm creating an XML file) and I want to have prefixes in my tags like "cac:" for instance. E.g.

<cac:Tax>
    <cbc:Amount>500.00</cbc:Amount>
</cac:Tax>

I am using an XmlWriter to create the xml, essentially doing it like so:

writer.WriteStartDocument()
writer.WriteStartElement("cac:Tax")
writer.WriteElementString("Amount", "500.00")
writer.WriteEndElement()
writer.WriteStartDocument()

But I get an error saying that ":" is an invalid character. How do I force it to use it anyway? I have tried the following

Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
settings.CheckCharacters = false

And I've also tried setting a prefix and namespace for the startElement:

writer.WriteStartElement("cac", "Tax", Nothing)  

But neither of those seemed to work... Well setting the prefix did, but only if I also declare a namespace, and I don't want that.

I want it to format it like xml but neglect any syntactic differences :)

Upvotes: 0

Views: 576

Answers (1)

Rasmus Eskesen
Rasmus Eskesen

Reputation: 246

Thanks to @Malcor and @JaggenSWE I found out that I had to declare the namespaces

writer.WriteAttributeString("xmlns","cbc", Nothing,"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
writer.WriteAttributeString("xmlns","cac", Nothing,"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")  

Not only did this do the trick, it is also exactly how you're supposed to do it, I just didn't realize at first.

Upvotes: 2

Related Questions