Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml element name with colon

Tags:

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.

like image 727
nelsonwebs Avatar asked Nov 24 '09 19:11

nelsonwebs


People also ask

Can a XML tag have colon?

XML Namespaces are based on the use of qualified names, similar to those long used in programming languages. Names are permitted to contain a colon, separating the name into two parts, the namespace name and the local name.

How do you put a colon in XML?

The colons indicate that those elements are in the ns1 namespace. This is needed when you are using multiple schemas. Assuming the document only uses ns1, it is essentially equivalent to not having any "ns1:" in those tags.

How do you escape a colon in XML?

Colon is a reserved character in XML identifiers, and may be used only for binding XML Namespaces. There is no way to escape it for other purposes.


1 Answers

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

like image 96
nelsonwebs Avatar answered Sep 22 '22 05:09

nelsonwebs