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.

get rid of blank namespaces

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lore Leunoeg - 19 Apr 2006 23:03 GMT
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

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.