Groups | Blog | Home
all groups > dotnet xml > may 2004 >

dotnet xml : assign dt as namespace for xmlelement


Matt Torline via .NET 247
5/4/2004 5:04:09 AM
I am trying to duplicate an xml document, and I am pretty new to xml. I am using hte Xml namespace and taking advantage of the xmldocument class. I would like to add the following to an element

xmlns:dt="urn:schemas-microsoft-com:datatypes"

Is there a way to do this through the setattribute or createelement methods? Whenever I try all i get is the xmlns="urn:schemas-microsoft-com:datatypes"


--------------------------------
From: Matt Torline

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

Martin Honnen
5/4/2004 2:57:43 PM


[quoted text, click to view]

You need to create an attribute node in the namespace
http://www.w3.org/2000/xmlns/, as in the following example:

using System;
using System.Xml;

public class Test20040504 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040504.xml");
XmlAttribute attribute = xmlDocument.CreateAttribute("xmlns", "dt",
"http://www.w3.org/2000/xmlns/");
attribute.Value = "urn:schemas-microsoft-com:datatypes";
xmlDocument.DocumentElement.SetAttributeNode(attribute);
Console.WriteLine(xmlDocument.OuterXml);
}
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oleg Tkachenko [MVP]
5/4/2004 4:10:14 PM
[quoted text, click to view]

Yeah, but don't expect this namespace declaration to be in effect on the
DOM in memory. The DOM needs to be reparsed again to make namespaces
changed.
--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button