473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CreateElement and xmlns empty attribute

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


Apr 28 '06 #1
3 12446


Fede wrote:

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/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/
Apr 28 '06 #2
> 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.


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

Apr 28 '06 #3


Fede wrote:
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(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/
Apr 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kevin | last post by:
I have a document, lets say: <Doc> <Ele1 Att1='hello'> <Ele2 Att2='goodbye'/> </Ele1> </Doc> In XSLT I need to generate an attribute value which contains part of this document as a text...
2
by: Jacques | last post by:
Is it possible to predicate by attribute, and if so, how? <System xmlns="" name="SAP_R3_Financials_DEV200"> <Attributes xmlns=""> <pcd:Attribute name="ApplicationServer" value="secacsap"/>...
0
by: chris.stromberger | last post by:
New to SOAP. I am working on something that uses the paypal web services. All the examples have this as the first portion of the soap doc <?xml version="1.0" encoding="UTF-8"?>...
1
by: rlcavebmg | last post by:
I need to generate an XML document that will be validated against a DTD. Up to this point, I have been using XML Serialization with good results, but there is one item I haven't been able to...
1
by: NeilL | last post by:
I'm trying to create an XML document that can be opened in Excel. I have some simple code that ... eventually does the following elem = _doc.CreateElement("Style");...
2
by: aglaforge | last post by:
I'm attempting to write a quick piece of Javascript code that will validate if the end user of the javascript has the necessary VML attributes set in their HTML. The problem in IE is that...
1
by: Paul J. Lucas | last post by:
I have code that builds a DOM using the Java org.w3c.dom API and it adds an extra attribute to one of the elements that I don't want. Not only that, the value it adds is wrong. The code to...
0
by: Ik | last post by:
Hi all, I use XslCompiledTranform to transform one XML file into another. In my stylesheet I use the msxsl:node-set() function, so I have a namespace import in my stylesheet. After...
4
by: jpdogg | last post by:
Hi all, I'll trying to tell the difference between the following three cases: <img alt="text string" /> <img alt="" /> <img /> I can do this in Firefox with the following code, where elem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.