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
Krzysztof Kazmierczak
Yingzi Le - 29 Aug 2005 23:43 GMT
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.
--------------------
>Thread-Topic: How to add XmlElement with a namespace
>thread-index: AcWs2qgEgMfEc9vSSKambaDeZcbAZw==
[quoted text clipped - 65 lines]
>Kind regards
>Krzysztof Kazmierczak
Krzysztof Kazmierczak - 30 Aug 2005 10:43 GMT
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
> Hi Krzysztof,
>
[quoted text clipped - 95 lines]
> >Kind regards
> >Krzysztof Kazmierczak
Martin Honnen - 30 Aug 2005 12:55 GMT
> I would like to have:
> coo.InnerXml = "<xsl:doo>test</xsl:doo>";
[quoted text clipped - 4 lines]
>
> 'xsl' is undeclared namespace
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>";
> Is that possible to create
> XmlDocument in memory with a namespace?
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" />

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/