473,237 Members | 1,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

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 8859
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
5
by: David Thielen | last post by:
Hi; I set up my xml as follows: XmlDocument xml = new XmlDocument(); xml.Load(File.Open("data.xml", FileMode.Open, FileAccess.Read)); XmlNamespaceManager context = new...
5
by: Yifan | last post by:
Hi Could any one tell me what project property controls the setting of '/RTC1' option? Now I have a project which has this build error "Command line error D2016 : '/RTC1' and '/clr' command-line...
11
by: Wolfgang Kaml | last post by:
I am not sure if this is more of an expert question, but I am sure that they are out there. I'd like to setup a general application or bin directory on my Win2003.Net Server that will hold some...
12
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST>
2
by: Mark | last post by:
Hi... I've been trying the .Validate() method on the XmlDocument to validate some xml against a schema, but one thing I noted was that unless the document explicitly declares the schema as a...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: David Veeneman | last post by:
I'm creating a UserControl that uses a LinkLabel. For reasons that I won't bore everyone with, I don't want the LinkLable to show the default hand cursor when the mouse enters the control. Should...
10
by: Brad Baker | last post by:
I have an asp.net/csharp application that requires a particular variable to work properly. This variable is usually passed via a query string in the URL when the application is first run but under...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.