views:

503

answers:

5

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"&gt;

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.

A: 

The word before the colon is the namespace. If you want to have an element "ns1:E", you needs to create an element named "E" and set its namespace to "ns1".

Kaleb Brasee
Thanks, makes sense but somehow I'm not getting the result I'm looking for yet. Not sure why. If I specify a uri or ns reference, the element name is correct but it adds the reference to the output which I don't want. If I don't, the prefix is not added.
nelsonwebs
+1  A: 

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. See this guide for more info.

John Paulett
Actually, those colons indicate that the elements are in the `schema` namespace. `ns1` is not the namespace; it is the namespace's abbreviation. It can change; the namespace's name can't - not without becoming a different namespace.
Robert Rossney
A: 

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]

nelsonwebs
A: 

The XML document you posted is not actually well-formed, because the ns2 abbreviation assigned to many of the elements is not associated with a namespace. Fixed, it might look like this:

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

The above XML document is semantically equivalent to this one:

<s:E xmlns:s="schema">
  <s:B>
    <S xmlns="my-namespace">
      <m:V xmlns:m="my-namespace">
        <s:Bl xmlns:s="my-namespace"/>
      </m:V>
    </S>
  </s:B>
</s:E>

And to this one:

<E xmlns="schema">
  <B xmlns="schema">
    <S xmlns="my-namespace">
      <V>
        <Bl/>
      </V>
    </S>
  </B>
</E>

In all three cases, the E and B elements are in the schema namespace, while the S, V, and Bl elements are in the my-namespace namespace.

Namespace prefixes are helpful, but strictly speaking they're unnecessary. You can create XML documents, like the last example, that use no prefixes, and that declare the namespace explicitly for every element.

If, in processing XML, you think you care what prefix a given element is using, you're almost certainly wrong. The only thing you care about is what namespace it belongs to. For instance, if I load any of those three documents into an XmlDocument, the following code will write out the 'Bl' element:

    XmlNamespaceManager ns = new XmlNamespaceManager(d.NameTable);
    ns.AddNamespace("a", "schema");
    ns.AddNamespace("b", "my-namespace");
    Console.Write(d.SelectSingleNode("/a:E/a:B/b:S/b:V/b:Bl", ns).OuterXml);

When you say:

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>.

you are almost certainly in error. An element whose tag is ns1:E is meaningless unless the ns1 prefix is mapped to a namespace, either in that element or in one of its ancestors. (Also, a namespace is not a schema reference.) If you use

CreateElement("ns1", "E", "schema");

to create the element, and then append it to an element that has already declared ns1 as being the prefix for the schema namespace, then the DOM will append the element without the namespace declaration, because in that context it isn't needed. If ns1 isn't declared (or is declared as abbreviating some namespace other than schema), then the DOM will stick a namespace declaration onto the element as well as the prefix.

tl;dr: You care about namespaces, not namespace prefixes.

Robert Rossney
A: 

Dear,

I have same problem I need XML Element without namespace, like below

any help how to create the these elements.

dim XMLCondition as XMLElement

XMLcondition=xmldoc.createElement("c:Condition")

output is only whereas i need

Regards,

Waseem