469,942 Members | 2,606 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 9683
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Jason | last post: by
6 posts views Thread by David Thielen | last post: by
3 posts views Thread by Jonathan | last post: by
1 post views Thread by Henry Padilla | last post: by
3 posts views Thread by =?Utf-8?B?bmVlZDJzY3ViYQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.