all groups > dotnet xml > august 2005 >
You're in the

dotnet xml

group:

How to add XmlElement with a namespace


How to add XmlElement with a namespace Krzysztof Kazmierczak
8/29/2005 1:45:57 PM
dotnet xml:
Hi All!

I have problem with creating xml document with a namespace:

XmlDocument xmlDocument = new XmlDocument();

XmlElement rootElement = xmlDocument.CreateElement("foo");
xmlDocument.AppendChild(rootElement);

XmlElement boo = xmlDocument.CreateElement("boo");
boo.InnerText = "boo";
rootElement.AppendChild(boo);

XmlElement coo = xmlDocument.CreateElement("coo");
coo.InnerXml = "<doo>test</doo>";
rootElement.AppendChild(coo);

I would like to have:
coo.InnerXml = "<xsl:doo>test</xsl:doo>";
instead of
coo.InnerXml = "<doo>test</doo>";

but I receive an error message:

'xsl' is undeclared namespace

I tried to add something like that:

XmlAttribute namespaceAttribute = xmlDocument.CreateAttribute("xmlns",
"xsl", "http://www.w3.org/2000/xmlns/");
namespaceAttribute.InnerText = "http://www.w3.org/1999/XSL/Transform";
rootElement.Attributes.Append(namespaceAttribute);

or

XmlAttribute namespaceAttribute = xmlDocument.CreateAttribute("xmlns",
"xsl", "http://www.w3.org/2000/xmlns/");
namespaceAttribute.Value = "http://www.w3.org/1999/XSL/Transform";
xmlDocument.DocumentElement.SetAttributeNode(namespaceAttribute);

but without effects, stil got error message. Is that possible to create
XmlDocument in memory with a namespace?

Thank you very much for any help
Kind regards
RE: How to add XmlElement with a namespace yingzile NO[at]SPAM online.microsoft.com
8/29/2005 10:43:44 PM

Hi Krzysztof,

The root element should have namespace declared. The following code works:

XmlDocument xmlDocument = new XmlDocument();

XmlDocument xmlDoc = new XmlDocument();
string rootElement = "<foo xmlns:xsl='urn:test'/>";

xmlDoc.Load(new StringReader(rootElement));

XmlElement boo = xmlDoc.CreateElement("boo");
boo.InnerText = "boo";
xmlDoc.DocumentElement.AppendChild(boo);

XmlElement coo = xmlDoc.CreateElement("coo");
XmlElement doo = xmlDoc.CreateElement("xsl","doo","urn:test");
doo.InnerText = "test";
coo.AppendChild(doo);
xmlDoc.DocumentElement.AppendChild(coo);
xmlDoc.Save(Console.Out);

Thanks,
-- Yingzi Le

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
--------------------
[quoted text, click to view]



RE: How to add XmlElement with a namespace Krzysztof Kazmierczak
8/30/2005 2:43:30 AM
Hi!

Thank you very much for your help! The code works great, but I still need to
do that thing:

instead of:
XmlElement coo = xmlDoc.CreateElement("coo");
XmlElement doo = xmlDoc.CreateElement("xsl","doo","urn:test");

I need:
XmlElement coo = xmlDoc.CreateElement("coo");
coo.InnerXml = "<xsl:doo>test</xsl:doo>";

And I receive the same error message saying that xsl is undeclared namespace
:(

Is that possible to add coo content that way?

Best regards and thank you very much for help
Krzysztof

[quoted text, click to view]
Re: How to add XmlElement with a namespace Martin Honnen
8/30/2005 1:55:58 PM


[quoted text, click to view]


[quoted text, click to view]

It obviously is so decide which namespace URI you want to use (for
instance http://example.com/2005/08/ns1) and then try (example C# code)
coo.InnerXml =
"<xsl:doo xmlns:xsl=\"http://example.com/2005/08/ns1\">test</xsl:doo>";

[quoted text, click to view]

Yes, sure, when you create an element you can directly create it in the
desired namespace by using an overload of CreateElement that takes three
arguments, the prefix, the local element name, and the namespace URI e.g.
XmlElement element =
someXmlDocument.CreateElement(
"prefix",
"localName",
"http://example.com/2005/08/ns1"
);
will create
<prefix:localName xmlns:prefix="http://example.com/2005/08/ns1" />

--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button