473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SelectSingleNode with a default namespace

My XML document has a default namespace specified by xmlns="some_url". Here
it is:

<?xml version="1.0" encoding="utf-8" ?>
<ssmproject
name="sample" server="sql"
xmlns="mailto:ne***********@msn.com?subject=ssmprj x"


<pr>
<script name="pr_sample_script_name" />
</pr>

</ssmproject>

I load it with XmlDocument.Load(filename). I have problem with
SelectSingleNode. The following code will return null:

m_doc.SelectSingleNode("/ssmproject");

I can work around this problem by using a Namespace manager and the 2nd form
of SelectSingleNode:

m_nsmgr = new XmlNamespaceManager(m_doc.NameTable);
m_nsmgr.AddNamespace("tns", "mailto:ne***********@msn.com?subject=ssmprjx" );

m_doc.SelectSingleNode("/tns:ssmproject", m_nsmgr);

But I have to add the prefix tns before every element in the XPath string,
e.g., /tns:ssmproject/tns:pr/tns:script. This is not good.

I'm not very familiar with XML namespace resolution. I was thinking that a
default namespace will do this:

m_nsmgr.AddNamespace(string.Empty,
"mailto:ne***********@msn.com?subject=ssmprjx" );

But I was wrong. Even I added this default namespace, the code

m_doc.SelectSingleNode("/ssmproject", m_nsmgr);

still returned null.

Was I missing something important?
Nov 11 '05 #1
2 9844
Edward Yang wrote:
My XML document has a default namespace specified by xmlns="some_url". Here
it is:

<?xml version="1.0" encoding="utf-8" ?>
<ssmproject
name="sample" server="sql"
xmlns="mailto:ne***********@msn.com?subject=ssmprj x"
<pr>
<script name="pr_sample_script_name" />
</pr>

</ssmproject>

I load it with XmlDocument.Load(filename). I have problem with
SelectSingleNode. The following code will return null:

m_doc.SelectSingleNode("/ssmproject");

I can work around this problem by using a Namespace manager and the 2nd form
of SelectSingleNode:

m_nsmgr = new XmlNamespaceManager(m_doc.NameTable);
m_nsmgr.AddNamespace("tns", "mailto:ne***********@msn.com?subject=ssmprjx" );

m_doc.SelectSingleNode("/tns:ssmproject", m_nsmgr);

But I have to add the prefix tns before every element in the XPath string,
e.g., /tns:ssmproject/tns:pr/tns:script. This is not good. This is the only way though.
I'm not very familiar with XML namespace resolution. I was thinking that a
default namespace will do this:

XPath 1.0 doesn't support default namespace unfortunately. XPath 2.0 does.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
OMG.

Things are not always (or always not) the same as what you think.

Thanks, it's helpful.

"Oleg Tkachenko" <ol***@multiconn.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Edward Yang wrote:
My XML document has a default namespace specified by xmlns="some_url". Here it is:

<?xml version="1.0" encoding="utf-8" ?>
<ssmproject
name="sample" server="sql"
xmlns="mailto:ne***********@msn.com?subject=ssmprj x"
<pr>
<script name="pr_sample_script_name" />
</pr>

</ssmproject>

I load it with XmlDocument.Load(filename). I have problem with
SelectSingleNode. The following code will return null:

m_doc.SelectSingleNode("/ssmproject");

I can work around this problem by using a Namespace manager and the 2nd form of SelectSingleNode:

m_nsmgr = new XmlNamespaceManager(m_doc.NameTable);
m_nsmgr.AddNamespace("tns", "mailto:ne***********@msn.com?subject=ssmprjx" );
m_doc.SelectSingleNode("/tns:ssmproject", m_nsmgr);

But I have to add the prefix tns before every element in the XPath string, e.g., /tns:ssmproject/tns:pr/tns:script. This is not good.

This is the only way though.
I'm not very familiar with XML namespace resolution. I was thinking that a default namespace will do this:

XPath 1.0 doesn't support default namespace unfortunately. XPath 2.0 does.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jason | last post by:
Hi I have an XML file i need to load and read the contents. Here is the top part of the xml file. <Research xsi:schemaLocation="http://www.rixml.org/2002/6/RIXML...
3
by: 455 | last post by:
Hello all, I've been trying to figure this out for hours now... can anyone help? I have an XML document like this: <?xml version="1.0" encoding="utf-8" ?> <ICE:ServiceCall xmlns="ICE"...
2
by: adam | last post by:
I tried so many ways to select the node but its not working, please help. I want to research by the identifier in imsmanifest.xml file, the node could be item or resource. XmlDocument doc = new...
6
by: David Thielen | last post by:
Hi; I am calling SelectSingleNode("/xml/s:Schema/s:ElementType/@name") where "/xml/s:Schema/s:ElementType/@name is a legit xpath statement (xml is the name of the rootnode) and that xpath...
3
by: Jonathan | last post by:
Hi, I use a XML-Doc with a Namespace like the following example: <HomedResources xmlns=http://schemas.microsoft.com/RtcServer/2002/11/dbimpexp Version="3"> <HomedResource...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
1
by: Henry Padilla | last post by:
I'm sorry if this has been asked and answered, I looked and didn't find this particular situation so I have to ask. If I understand correctly, the .NET XMLDOM must have an XmlNamespaceManager...
5
by: anupamjain | last post by:
Tired, Exhausted, searched the web, usenets,forums thorughly but still clueless. I guess it's time to post on the group : This is the issue I have been trying to resolve since today morning : ...
3
by: =?Utf-8?B?bmVlZDJzY3ViYQ==?= | last post by:
I have the following code snipet: .... _xmldocManifest.Load(strManifestAbsolutePath) Dim manifestNSManager As XmlNamespaceManager = New XmlNamespaceManager(_xmldocManifest.NameTable)...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.