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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With