xmlElement and namespace

C

Christoph

I'm using the following code to create what will be the root element of my
XML document.

XmlDocument controlDocument = new XmlDocument();
//add root node
XmlElement rootNode = controlDocument.CreateElement( "ns0:MyRootElement" );
rootNode.SetAttribute( "xmlns:ns0", "http://my.namespace.uri" );
controlDocument.AppendChild(rootNode);

I'm trying to get it so that it comes out as follows:

<ns0:MyRootElement xmlns:ns0="http://my.namespace.uri">
</ns0:MyRootElement>

however, when I'm print out the XML document, it's only printing out as
such:

<MyRootElement xmlns:ns0="http://my.namespace.uri">
</MyRootElement>

Why is it getting rid of the beginning 'ns0:'? And what can I do to keep it
in there?

thnx,
Christoph
 
G

Guest

try to change you code to the following:

XmlDocument controlDocument = new XmlDocument();

//add root node, ADD THE NAMESPACE IN THE CREATE ELEMENT method

XmlElement rootNode = controlDocument.CreateElement( "ns0:MyRootElement",
"http://my.namespace.uri" );

//don't do this
//rootNode.SetAttribute( "xmlns:ns0", "http://my.namespace.uri" );

controlDocument.AppendChild(rootNode);


Hope that helps
Mark.
 
C

Christoph

try to change you code to the following:
XmlDocument controlDocument = new XmlDocument();
//add root node, ADD THE NAMESPACE IN THE CREATE ELEMENT method
XmlElement rootNode = controlDocument.CreateElement( "ns0:MyRootElement",
"http://my.namespace.uri" );
//don't do this
//rootNode.SetAttribute( "xmlns:ns0", "http://my.namespace.uri" );
controlDocument.AppendChild(rootNode);
Hope that helps

That was an incredible help!
Thank you so very much! :) It is very much appreciated.

thnx,
Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top