469,286 Members | 2,425 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

setting default namespace ??

How can I set the default namespace of an XmlDocument/XmlElement?
I've tried doing something like

rootElement.SetAttribute( "xmlns", "http://www.w3.org/2000/xmlns/",
"http://www.some.org/theschema" );

on the DocumentElement. However, this fails. Any ideas??

Thanks.
Joost
Nov 12 '05 #1
6 8652
JoostV wrote:
How can I set the default namespace of an XmlDocument/XmlElement?


You cannot set namespace of document in memory. You can add namespace
declaration (by just adding as attribute), but it'll be in effect only
when document is reparsed again.
Instead try to accomplish it while XmlDocument is loaded, because
namespace is integral part of a node and cannot be changed without
recreating the node.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2
What is the failure (exception text) ? You should be able to set an
attribute representing a namespace node on any element, including the root
element.

--
Daniel D.C. [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights
"JoostV" <Jo*******@hotmail.com> wrote in message
news:ef**************************@posting.google.c om...
How can I set the default namespace of an XmlDocument/XmlElement?
I've tried doing something like

rootElement.SetAttribute( "xmlns", "http://www.w3.org/2000/xmlns/",
"http://www.some.org/theschema" );

on the DocumentElement. However, this fails. Any ideas??

Thanks.
Joost

Nov 12 '05 #3

The error message is "The namespace declaration attribute has an
incorrect namespaceURI: http://www.my.org/MYSchema_1_1."

Joost
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #4
Sorry for the late reply. This error happens when the code is trying to add
a namespace attribute and has a mistmatched namespace for the xmlns "prefix"
(i.e. not giving "http://www.w3.org/2000/xmlns/" as the namespace for the
xmlns attribute / namespace node).
However the code fragment you gave in your first post works fine, so I am at
loss why this error occurs. Perhaps, some other part of the code executes a
SetAttribute for a namepsace element without giving the
"http://www.w3.org/2000/xmlns/" string for the namespace parameter ?
--
Daniel D.C. [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights

"Joost News" <jo*******@hotmail.com> wrote in message
news:eA*************@TK2MSFTNGP11.phx.gbl...

The error message is "The namespace declaration attribute has an
incorrect namespaceURI: http://www.my.org/MYSchema_1_1."

Joost
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 12 '05 #5
I know this is an old post but just in case anyone is searching...

string myNS =
"http://mydomain.com/URL";
string xsiNamespace =
"http://www.w3.org/2001/XMLSchema-instance";

XmlDocument doc = new XmlDocument();

doc.AppendChild(doc.CreateXmlDeclaration("1.0",
"UTF-8", string.Empty));
XmlNode root =
doc.AppendChild(doc.CreateElement("TestRoot",
myNS));

// adding the custom xmlns attribute... the URL on the next line is
CRITICAL!
XmlAttribute attr = doc.CreateAttribute("xmlns",
"xsi", "http://www.w3.org/2000/xmlns/");
attr.InnerText = xsiNamespace;
root.Attributes.Append(attr);

attr = doc.CreateAttribute("schemaLocation",
xsiNamespace);
attr.InnerText =
string.Format(@"{0}/SaleComparableSearchCriteria.xsd",
myNS);
root.Attributes.Append(attr);

XmlNode spNode =
root.AppendChild(doc.CreateElement("SubjectPropert y",
myNS));
XmlElement elSP = doc.CreateElement("Latitude",
myNS);
elSP.SetAttribute("nil", xsiNamespace,
"true");
spNode.AppendChild(elSP);

...

hope this helps.

Fred
Daniel D.C. [MSFT]wrote: "]Sorry for the late reply. This error happens when the code is
trying to add a namespace attribute and has a mistmatched namespace for the xmlns "prefix" (i.e. not giving "http://www.w3.org/2000/xmlns/" as the namespace for the xmlns attribute / namespace node).
However the code fragment you gave in your first post works fine, so I am at loss why this error occurs. Perhaps, some other part of the code executes a SetAttribute for a namepsace element without giving the
"http://www.w3.org/2000/xmlns/" string for the namespace parameter ?

--
Daniel D.C. [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights
"Joost News" <jo*******@hotmail.com> wrote in message
news:eA*************@TK2MSFTNGP11.phx.gbl...

The error message is "The namespace declaration attribute has an
incorrect namespaceURI: http://www.my.org/MYSchema_1_1."

Joost
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for

it![/quote:c718b05641][code:1:c718b05641][/code:1:c718b05641]

Nov 12 '05 #6
Thanks for this sample...

This helped a lot to understand namespaces in dotnet !!
Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike Dickens | last post: by
5 posts views Thread by David Thielen | last post: by
4 posts views Thread by David Veeneman | last post: by
10 posts views Thread by Brad Baker | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.