xml element name with colon

nelsonwebs picture nelsonwebs · Nov 24, 2009 · Viewed 42.4k times · Source

I'm working against a 3rd party xml api. They have defined a required xml structure similar to the following.

<ns1:E xmlns:ns1="schema">
<ns1:B>
    <ns2:S>
        <ns2:V>
            <ns2:Bl />
        </ns2:V>
    </ns2:S>
</ns1:B>
</ns1:E>

There is a SQL table with the information that I need to put into this xml format. I have a LINQ to SQL adapter to get the data and I'm using a System.Xml to create an XML document.

XmlDocument.CreateElement("ns1:E"); etc

This works fine as long as I remove the colons from the element names. With the colons, only the right hand side of the colon is in the element name. I know colons are a no-no but I don't have any control over what the 3rd party api is dictating.

What are my options for getting around this? Are there any useful ways of forcing the colons into the element names? I don't have to use XMLDocument but not sure what other method will get me there.

UPDATE: I realize that the <ns1: refers to a namespace. And yes, there are 2. When writing out the XML, I can make it work if I say -

 XmlDocument.CreateElement("ns1:E", "http://schema");

However, the XML output of this is

<ns1:E xmlns:ns1="http://schema">

If I just say XmlDocument.CreateElement("ns1:E"); with no uri, then the output is just <E>. I don't want the output to have the schema reference but I do need to have the prefix. The result I want to achieve is simply <ns1:E>. Both namspaces are declared at the top which I would think would mean I would have to declare them at every node.

Answer

nelsonwebs picture nelsonwebs · Nov 24, 2009

Okay, here it is.

XmlDocument.CreateElement("prefix", "name", "uri");

reference here if it helps someone else: http://msdn.microsoft.com/en-us/library/c22k3d47.aspx 1