Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / April 2006

Tip: Looking for answers? Try searching our database.

CreateElement and  xmlns empty attribute

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fede - 28 Apr 2006 10:47 GMT
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 - 28 Apr 2006 12:36 GMT
> in vb.net I have this code:
> .....
> myNode= XmlDoc.CreateElement("aNode")

> <aNode xlink:href="foo" xmlns="">

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/frlr
fSystemXmlXmlDocumentClassCreateElementTopic2.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.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Fede - 28 Apr 2006 13:21 GMT
> That happends during serialization if there is an
> xmlns="http://example.com/" declaration on an ancestor element. Thus if
[quoted text clipped - 3 lines]
> That way the serialization will work as you want it and no xmlns="" will
> occur.

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 - 29 Apr 2006 13:12 GMT
> the problem is that i don't want  xmlns attribute in my xml neither empty
> neither with a value, how can i do?

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.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.