473,387 Members | 1,575 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,387 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 8869
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.