Hello
I'm using c# XmlDocument class to add new XHTML-Nodes to my website.
Unfortunately XmlDocument always adds an unwanted empty namespace attribute
xmlns="" to every new Element.
These empty namespace attributes cause that the elements are'nt found any
more when I'm reparsing the document. (When I delete them by hand everything
works fine).
This is really stupid. DotNet XML-classes aren't very useful for me if I
can't affect their behaviour.
XmlSerializer seems to have the same problem.
Isn't there any hack to bypass the blank namespaces???
Thank you
Sincerely
Lore
******************* code ***************************
//doc is a loaded XmlDocument object
XmlElement paragraph = doc.CreateElement("p");
paragraph.SetAttribute("class","paragraph");
XmlElement imgNode = null;
if(pctEntry.Image != null)
{
imgNode = doc.CreateElement("img");
imgNode.SetAttribute("src",pctEntry.Tag.ToString());
imgNode.SetAttribute("alt",txtAlt.Text);
paragraph.AppendChild(imgNode);
}
//text
if(txtEntry.Text.Trim() != "")
{
string tmp = ToXHTML(txtEntry.Text);
XmlText textNode = doc.CreateTextNode(tmp);
paragraph.AppendChild(textNode);
}
XmlNode node = doc.SelectSingleNode("//xl:div[@ id='content']",
namespaceManager);
if(node != null)
node.AppendChild(paragraph);
Martin Honnen - 20 Apr 2006 13:36 GMT
> I'm using c# XmlDocument class to add new XHTML-Nodes to my website.
> Unfortunately XmlDocument always adds an unwanted empty namespace attribute
> xmlns="" to every new Element.
If you want to create elements in the XHTML 1 namespace with URI
http://www.w3.org/1999/xhtml with the DOM then of course you need to use
namespace aware methods and pass in the right namespace e.g. with that
DOM code
XmlDocument xmlDocument = new XmlDocument();
const string xhtml1NamespaceURI = "http://www.w3.org/1999/xhtml";
xmlDocument.AppendChild(xmlDocument.CreateElement("html",
xhtml1NamespaceURI));
xmlDocument.DocumentElement.AppendChild(xmlDocument.CreateElement("head",
xhtml1NamespaceURI));
xmlDocument.DocumentElement.AppendChild(xmlDocument.CreateElement("body",
xhtml1NamespaceURI));
xmlDocument.Save(Console.Out);
the result is
<html xmlns="http://www.w3.org/1999/xhtml">
<head />
<body />
</html>
So make sure _when_ you create an element you pass in the right
namespace URI to CreateElement as the namespace a node belongs to is
determined when the node is created and not when/where it is inserted later.

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Lore Leunoeg - 21 Apr 2006 23:33 GMT
Mmh. Thank you.
Seems I'm a little dull with namespaces.
Thank you again.
Sincerely
Lore