all groups > dotnet xml > april 2006 >
You're in the

dotnet xml

group:

CreateElement and xmlns empty attribute



CreateElement and xmlns empty attribute Fede
4/28/2006 12:00:00 AM
dotnet xml: Hi everybody,
in vb.net I have this code:
......
myNode= XmlDoc.CreateElement("aNode")

myAttribute = XmlDoc.CreateAttribute("xlink", "href",
"http://www.w3.org/1999/xlink")

myAttribute.Value ="foo"

myNode.Attributes.Append(myAttribute)

......

Purtroppo quello che ottengo è:
<aNode xlink:href="foo" xmlns="">

How can I avoid the default empty attribute xmlns?

Thank you in advance for any suggest.
Fede



Re: CreateElement and xmlns empty attribute Martin Honnen
4/28/2006 1:36:15 PM


[quoted text, click to view]


[quoted text, click to view]

That happends during serialization if there is an
xmlns="http://example.com/" declaration on an ancestor element. Thus if
you want to be aNode in a particular namespace then you need to use an
overload of CreateElement that allows you to set the namespace e.b.
myNode = XmlDoc.CreateElement("aNode", "http://example.com/");
That way the serialization will work as you want it and no xmlns="" will
occur.

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlDocumentClassCreateElementTopic2.asp>

The namespace a node is in is determined when you create the node so
always use namespace aware methods to create an element or attribute if
namespaces matter.

--

Martin Honnen --- MVP XML
Re: CreateElement and xmlns empty attribute Fede
4/28/2006 2:21:21 PM
[quoted text, click to view]

the problem is that i don't want xmlns attribute in my xml neither empty
neither with a value, how can i do?


Re: CreateElement and xmlns empty attribute Martin Honnen
4/29/2006 2:12:30 PM


[quoted text, click to view]

Well then make sure there is no element in the DOM tree that is in a
namespace. If the serializer does e.g.
xmlns=""
when serializing your DOM tree then that means there is an element in
the DOM tree which is in a namespace.
So somewhere up the tree you have an element in a namespace, if you
don't want that then don't create it at all or remove it before serializing.

For instance with the code

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateElement("root"));

xmlDocument.DocumentElement.AppendChild(xmlDocument.CreateElement("child"));
xmlDocument.Save(Console.Out);

the result is

<root>
<child />
</root>

so you can see there is no xmlns="" declaration. However if the code is e.g.

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateElement("root",
"http://example.com/2006/ns1"));

xmlDocument.DocumentElement.AppendChild(xmlDocument.CreateElement("child"));
xmlDocument.Save(Console.Out);

then the result is

<root xmlns="http://example.com/2006/ns1">
<child xmlns="" />
</root>

You can see that the creation of the element with tag name "child" is
the same in both examples, only the serialization differs as the parent
element is in a namespace in the second example. That means you have to
look at the ancestors of the element where you get xmlns="" and make
sure that the ancestor is in no namespace.



--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button