468,242 Members | 1,635 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

XML Elements have the same attributes

Hello,

I am creating an XML Document using XmlDocument.
I can create my top node no problem

However when I add child nodes they seem to inherit the xmlns=""
attribute (see xml below).
How can I stop this?

-------------------------
Code snippet
-------------------------

n = xmlDoc.CreateElement("nl", "MyXML", "http://www.myurl.com");
XmlAttribute nns = xmlDoc.CreateAttribute( "xmlns" );
nns.InnerText = @"www.myurl.com/xml";n.Attributes.SetNamedItem( nns );
xmlDoc.AppendChild(n);

XmlNode TemplateNode;
TemplateNode = xmlDoc.CreateElement("Template");
n.AppendChild(TemplateNode);

---------------------
Resulting XML
---------------------

<?xml version="1.0" encoding="UTF-8"?>
<nl:MyXML xmlns="http://www.myurl.com/xml"
xmlns:vl="http://www.myurl.com">
<Template xmlns="">

</Template>
</vl:VNETList>

Feb 1 '06 #1
1 1221


ll**********@yahoo.com wrote:

n = xmlDoc.CreateElement("nl", "MyXML", "http://www.myurl.com");
That is fine if you want to have that element in a certain namespace.
XmlAttribute nns = xmlDoc.CreateAttribute( "xmlns" );
nns.InnerText = @"www.myurl.com/xml";n.Attributes.SetNamedItem( nns );
That does not help, consider removing that.
xmlDoc.AppendChild(n);

XmlNode TemplateNode;
TemplateNode = xmlDoc.CreateElement("Template");


If you want that Template element to be in the namespace with the URI
then use the proper overload of CreateElement e.g.
TemplateNode = xmlDoc.CreateElement(
"",
"Template",
"www.myurl.com/xml"
)
You have to do that for each element to be created in a certain
namespace. Later when the DOM document is being serialized the
serializer will take care of adding namespace declaration attributes.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 1 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jakub.Bednarczuk | last post: by
10 posts views Thread by Andy Fish | last post: by
reply views Thread by David Lozzi | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.