browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need .NET Framework help?

Get answers from our community of .NET Framework experts on BYTES! It's free.

CreateElement and xmlns empty attribute

Fede
Guest
 
Posts: n/a
#1: Apr 28 '06
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







Martin Honnen
Guest
 
Posts: n/a
#2: Apr 28 '06

re: CreateElement and xmlns empty attribute




Fede wrote:

[color=blue]
> in vb.net I have this code:
> .....
> myNode= XmlDoc.CreateElement("aNode")[/color]
[color=blue]
> <aNode xlink:href="foo" xmlns="">[/color]

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
http://JavaScript.FAQTs.com/
Fede
Guest
 
Posts: n/a
#3: Apr 28 '06

re: CreateElement and xmlns empty attribute


> That happends during serialization if there is an[color=blue]
> 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.[/color]

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



Martin Honnen
Guest
 
Posts: n/a
#4: Apr 29 '06

re: CreateElement and xmlns empty attribute




Fede wrote:
[color=blue]
> the problem is that i don't want xmlns attribute in my xml neither empty
> neither with a value, how can i do?[/color]

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(xmlDocumen t.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(xmlDocumen t.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
http://JavaScript.FAQTs.com/
Closed Thread