Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 25th, 2006, 09:45 AM
Dan Kelley
Guest
 
Posts: n/a
Default Embarrasingly easy question

I need to process a very simple xml document, but as it contains a namespace
I am finding this very difficult.

The xml is structured as follows:
<results xmlns="http://fictional.namespace.com/md">
<result status="true" />
<result status="true" />
<result status="false" error="error" />
</results>

This xml is being returned from a web service as an XmlNode. Normally I
would be able to use the code:

XmlNodeList results = returnInfo.SelectNodes("results");
for (int index = 0; index < results.Count; index++)
{
bool success =
XmlConvert.ToBoolean(results[index].SelectSingleNode("status").Value);
//process as appropriate
}

However, the presence of the namespace means the list always has a count of
0. From the docs I can see SelectNodes has an overload that additionally
accepts an XmlNamespaceManager parameter, but I cannot get this to work
either.

Thanks in advance.
Dan
  #2  
Old July 25th, 2006, 10:05 AM
George Bina
Guest
 
Posts: n/a
Default Re: Embarrasingly easy question

Hi,

You need to declare in the namespace manager a prefix for your
namespace and then use that prefix to qualify the references to your
element, for instance if you use md as prefix then use md:results
instead results when you call the select nodes method.
Alternatively you can use *[local-name()='results'] to ignore the
namespace but this will select any results element no matter its
namespace. You can check also the namespace if you use
*[local-name()='results' and
namespace-uri()='http://fictional.namespace.com/md']

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Dan Kelley wrote:
Quote:
I need to process a very simple xml document, but as it contains a namespace
I am finding this very difficult.
>
The xml is structured as follows:
<results xmlns="http://fictional.namespace.com/md">
<result status="true" />
<result status="true" />
<result status="false" error="error" />
</results>
>
This xml is being returned from a web service as an XmlNode. Normally I
would be able to use the code:
>
XmlNodeList results = returnInfo.SelectNodes("results");
for (int index = 0; index < results.Count; index++)
{
bool success =
XmlConvert.ToBoolean(results[index].SelectSingleNode("status").Value);
//process as appropriate
}
>
However, the presence of the namespace means the list always has a count of
0. From the docs I can see SelectNodes has an overload that additionally
accepts an XmlNamespaceManager parameter, but I cannot get this to work
either.
>
Thanks in advance.
Dan
  #3  
Old July 25th, 2006, 12:05 PM
Dan Kelley
Guest
 
Posts: n/a
Default Re: Embarrasingly easy question

Thanks for your help. My problem is now solved.

Dan

"George Bina" wrote:
Quote:
Hi,
>
You need to declare in the namespace manager a prefix for your
namespace and then use that prefix to qualify the references to your
element, for instance if you use md as prefix then use md:results
instead results when you call the select nodes method.
Alternatively you can use *[local-name()='results'] to ignore the
namespace but this will select any results element no matter its
namespace. You can check also the namespace if you use
*[local-name()='results' and
namespace-uri()='http://fictional.namespace.com/md']
>
Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
>
Dan Kelley wrote:
Quote:
I need to process a very simple xml document, but as it contains a namespace
I am finding this very difficult.

The xml is structured as follows:
<results xmlns="http://fictional.namespace.com/md">
<result status="true" />
<result status="true" />
<result status="false" error="error" />
</results>

This xml is being returned from a web service as an XmlNode. Normally I
would be able to use the code:

XmlNodeList results = returnInfo.SelectNodes("results");
for (int index = 0; index < results.Count; index++)
{
bool success =
XmlConvert.ToBoolean(results[index].SelectSingleNode("status").Value);
//process as appropriate
}

However, the presence of the namespace means the list always has a count of
0. From the docs I can see SelectNodes has an overload that additionally
accepts an XmlNamespaceManager parameter, but I cannot get this to work
either.

Thanks in advance.
Dan
>
>
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles