473,399 Members | 3,401 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,399 software developers and data experts.

SelectNodes() having trouble...

I have the following xml doc...

<?xml version="1.0"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<Body>
<StatusReport>
<StatusRecord>
<CorrelationID>E1308AB43036D8B8FCD2CBB1EE7FFA6B</CorrelationID>
</StatusRecord>
<StatusRecord>
<CorrelationID>CDDDD8EEBAFAE41A71FF24A3C36E7467</CorrelationID>
</StatusRecord>
</StatusReport>
</Body>
</GovTalkMessage>

I'm trying to get a list of the CorrelationID elements but can't seem to get
SelectNodes to find anything..

------------------------
XMLDoc.LoadXml(XMLstring)
nodeList =
XMLDoc.SelectNodes("/GovTalkMessage/Body/StatusReport/StatusRecord/CorrelationID")
For Each CorrelationID In nodeList
' do something with CorrelationID.Value
Next
------------------------

What am I doing wrong ?

Many Thanks
Adrian

Nov 12 '05 #1
2 4135
You will need to associate a namespace prefix with the default namespace
that you are using in the
document(xmlns="http://www.govtalk.gov.uk/CM/envelope").
You do that by connecting an XmlNamespaceManager to the name table of your
XmlDocument,
and then specify a prefix that you want to use for that namespace URI.
I created an example, using "default" as the namespace prefix, seen below.
"default" should then be used in XPath evaluations as a prefix on any node
that uses that particular namespace(although in your case you use only one
namespace URI, there could be more than one).
Example:
XmlDocument doc = new XmlDocument();
doc.LoadXml(XMLstring);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("default", "http://www.govtalk.gov.uk/CM/envelope");
// NOtice we use "default" as prefix in the XPath.
XmlNode node = doc.DocumentElement.SelectSingleNode
(
"default:Body/default:StatusReport/default:StatusRecord/default:CorrelationID"
, nsmgr );
Console.WriteLine(node.OuterXml);
Console.Read();
HTH.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Adrian Parker" <ap******@nospam.com> wrote in message
news:Oy**************@tk2msftngp13.phx.gbl...
I have the following xml doc...

<?xml version="1.0"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<Body>
<StatusReport>
<StatusRecord>

<CorrelationID>E1308AB43036D8B8FCD2CBB1EE7FFA6B</CorrelationID>
</StatusRecord>
<StatusRecord>

<CorrelationID>CDDDD8EEBAFAE41A71FF24A3C36E7467</CorrelationID>
</StatusRecord>
</StatusReport>
</Body>
</GovTalkMessage>

I'm trying to get a list of the CorrelationID elements but can't seem to
get SelectNodes to find anything..

------------------------
XMLDoc.LoadXml(XMLstring)
nodeList =
XMLDoc.SelectNodes("/GovTalkMessage/Body/StatusReport/StatusRecord/CorrelationID")
For Each CorrelationID In nodeList
' do something with CorrelationID.Value
Next
------------------------

What am I doing wrong ?

Many Thanks
Adrian

Nov 12 '05 #2
Many thanks Dennis, that was what I needed.
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:nf****************@news4.e.nsc.no...
You will need to associate a namespace prefix with the default namespace
that you are using in the
document(xmlns="http://www.govtalk.gov.uk/CM/envelope").
You do that by connecting an XmlNamespaceManager to the name table of your
XmlDocument,
and then specify a prefix that you want to use for that namespace URI.
I created an example, using "default" as the namespace prefix, seen below.
"default" should then be used in XPath evaluations as a prefix on any node
that uses that particular namespace(although in your case you use only one
namespace URI, there could be more than one).
Example:
XmlDocument doc = new XmlDocument();
doc.LoadXml(XMLstring);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("default", "http://www.govtalk.gov.uk/CM/envelope");
// NOtice we use "default" as prefix in the XPath.
XmlNode node = doc.DocumentElement.SelectSingleNode
(
"default:Body/default:StatusReport/default:StatusRecord/default:CorrelationID"
, nsmgr );
Console.WriteLine(node.OuterXml);
Console.Read();
HTH.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Adrian Parker" <ap******@nospam.com> wrote in message
news:Oy**************@tk2msftngp13.phx.gbl...
I have the following xml doc...

<?xml version="1.0"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<Body>
<StatusReport>
<StatusRecord>

<CorrelationID>E1308AB43036D8B8FCD2CBB1EE7FFA6B</CorrelationID>
</StatusRecord>
<StatusRecord>

<CorrelationID>CDDDD8EEBAFAE41A71FF24A3C36E7467</CorrelationID>
</StatusRecord>
</StatusReport>
</Body>
</GovTalkMessage>

I'm trying to get a list of the CorrelationID elements but can't seem to
get SelectNodes to find anything..

------------------------
XMLDoc.LoadXml(XMLstring)
nodeList =
XMLDoc.SelectNodes("/GovTalkMessage/Body/StatusReport/StatusRecord/CorrelationID")
For Each CorrelationID In nodeList
' do something with CorrelationID.Value
Next
------------------------

What am I doing wrong ?

Many Thanks
Adrian


Nov 12 '05 #3

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

Similar topics

5
by: Alexander Gnauck | last post by:
Hello, i have problems with the Namespaces and the .Net XML Parser My XML looks like this: <query xmlns="jabber:iq:roster"> <item jid="srlee@localhost" name="srlee"...
8
by: e-mid | last post by:
i want to get childs of specific xml node. normally i use xmlNodeList fooList = myNode.SelectNodes("foo"); but in compactframework , there is no selectNodes() method or selectSingleNode()...
2
by: | last post by:
I am having a bit of a brainfart here at 4am in the morning. I have XmlNodeList TmpServers = ServerNodes.SelectNodes(""); and I have also tried XmlNodeList TmpServers =...
1
by: Riko Eksteen | last post by:
Hi I am writing an application that listens to the NodeInserted event, and if the event is fired it does a selectnodes xpath query on the XmlDocument. Only problem is, when I call...
2
by: Michael H | last post by:
Hello group, I have a some xml that looks like this below: <tuneRequests ct="3" xmlns:sql="urn:schemas-microsoft-com:xml-sql" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <tr...
2
by: Linda Boumarafi | last post by:
Hi, This is a newbie question to XMLNS. I just finished the article on "XML Namespaces and How They Affect XPath and XSLT" by Dare Obasanjo and I'm pretty sure I understand the problem. I just...
2
by: Joe | last post by:
Hello All: Does anyone know the differnce between the GetElementsByTagName method and the SelectNodes method? I know that they take different arguments. They also both return a NodeList. I'm...
2
by: DeveloperX | last post by:
SelectNodes under 1.1 is confusing me. Imagine an xml file that looks like this. It's just an example: <drive> <dir a="a"> <file>aA</file> <file>aB</file> <dir a="ab"> <file>abA</file> <dir...
3
by: John Smith | last post by:
I'm trying to make some Javascript X-browser. foo is a HTML DOM. I have foo.documentElement.selectNodes("xpath"); in the source. I see later down there is also a foo.transformNode. Yuck. ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.