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

Home Posts Topics Members FAQ

SelectSingleNode always selects same node when looping through

Dear All,

I am repeatedly encountering a problem whilst looping through XML
Nodes and I am unsure as to what is going on and how I can get around
it.

I load the following XML document into an XmlDocument object using
LoadXml:

<?xml version="1.0" encoding="UTF-8"?>
<Templates>
<Template Filename="NON.RTN.FORMA3.xml">
<SQLCommand>SELECT Addresses.Address_Line1, Addresses.Address_Line2,
Addresses.Address_Line3, Addresses.Address_Line4, FROM
Addresses</SQLCommand>
</Template>
<Template Filename="ERChild.NON.RTN.xml">
<SQLCommand>SELECT Addresses.Address_Line1, Addresses.Address_Line2,
Addresses.Address_Line3, Addresses.Address_Line4, Addresses.Postcode
FROM Addresses</SQLCommand>
</Template>
</Templates>

XmlDocument xdTemplateInfo = new XmlDocument();
xdTemplateInfo.LoadXml(strJobXml);

(strJobXml contains the Xml as above). I then would like to retrieve
the SQLCommand for a Template whose filename attribute is the same as
my search string (strSearchFile). I thought that I could do this as
follows:

XmlNodeList xlTemplates =
xdTemplateData.SelectNodes("Templates/Template");
foreach (XmlNode xnTemplate in xlTemplates)
{
if (xnTemplate.Attributes.GetNamedItem("Filename").In nerText ==
strSearchFile)
{
//retrieve the SQL
//TODO: the following statement appears to always return the first
//matching node of the document!
XmlNode xnSql = xnTemplate.SelectSingleNode("/Templates/Template/SQLCommand");
strSQLCommand.Append(xnSql.InnerText);
break;
}
}

However, even though xnTemplate refers to a SingleNode in xlTemplates
(the list of Template nodes) the SelectSingleNode operation always
returns the first matching SQLCommand element in the XmlDocument i.e.
the SQL Command for the template with filename NON.RTN.FORMA3.xml. I
would like to know why the SelectSingleNode does not apply to the
current node but the whole document and, is there a way to select a
sub-node of a current node?

many thanks

Claire
Nov 12 '05 #1
2 6463
Because your XPath expression starts with /, which takes the context all the
way to the root node and runs the selection from that context. If you want
to select SQLCommand node that's a child of xnTemplate then you need to
search for "./SQLCommand" or simply "SQLCommand".

Jerry

"Claire Reed" <cl*********@yahoo.co.uk> wrote in message
news:c6*************************@posting.google.co m...
Dear All,

I am repeatedly encountering a problem whilst looping through XML
Nodes and I am unsure as to what is going on and how I can get around
it.

I load the following XML document into an XmlDocument object using
LoadXml:

<?xml version="1.0" encoding="UTF-8"?>
<Templates>
<Template Filename="NON.RTN.FORMA3.xml">
<SQLCommand>SELECT Addresses.Address_Line1, Addresses.Address_Line2,
Addresses.Address_Line3, Addresses.Address_Line4, FROM
Addresses</SQLCommand>
</Template>
<Template Filename="ERChild.NON.RTN.xml">
<SQLCommand>SELECT Addresses.Address_Line1, Addresses.Address_Line2,
Addresses.Address_Line3, Addresses.Address_Line4, Addresses.Postcode
FROM Addresses</SQLCommand>
</Template>
</Templates>

XmlDocument xdTemplateInfo = new XmlDocument();
xdTemplateInfo.LoadXml(strJobXml);

(strJobXml contains the Xml as above). I then would like to retrieve
the SQLCommand for a Template whose filename attribute is the same as
my search string (strSearchFile). I thought that I could do this as
follows:

XmlNodeList xlTemplates =
xdTemplateData.SelectNodes("Templates/Template");
foreach (XmlNode xnTemplate in xlTemplates)
{
if (xnTemplate.Attributes.GetNamedItem("Filename").In nerText ==
strSearchFile)
{
//retrieve the SQL
//TODO: the following statement appears to always return the first
//matching node of the document!
XmlNode xnSql =
xnTemplate.SelectSingleNode("/Templates/Template/SQLCommand");
strSQLCommand.Append(xnSql.InnerText);
break;
}
}

However, even though xnTemplate refers to a SingleNode in xlTemplates
(the list of Template nodes) the SelectSingleNode operation always
returns the first matching SQLCommand element in the XmlDocument i.e.
the SQL Command for the template with filename NON.RTN.FORMA3.xml. I
would like to know why the SelectSingleNode does not apply to the
current node but the whole document and, is there a way to select a
sub-node of a current node?

many thanks

Claire

Nov 12 '05 #2
Thanks Jerry, that worked. I always have problems knowing what the
correct XPath statement should be so thanks for pointing me in the
right direction.

cheers

Claire

"Jerry Pisk" <je******@hotmail.com> wrote in message news:<eT**************@TK2MSFTNGP10.phx.gbl>...
Because your XPath expression starts with /, which takes the context all the
way to the root node and runs the selection from that context. If you want
to select SQLCommand node that's a child of xnTemplate then you need to
search for "./SQLCommand" or simply "SQLCommand".

Jerry

"Claire Reed" <cl*********@yahoo.co.uk> wrote in message
news:c6*************************@posting.google.co m...
Dear All,

I am repeatedly encountering a problem whilst looping through XML
Nodes and I am unsure as to what is going on and how I can get around
it.

I load the following XML document into an XmlDocument object using
LoadXml:

<?xml version="1.0" encoding="UTF-8"?>
<Templates>
<Template Filename="NON.RTN.FORMA3.xml">
<SQLCommand>SELECT Addresses.Address_Line1, Addresses.Address_Line2,
Addresses.Address_Line3, Addresses.Address_Line4, FROM
Addresses</SQLCommand>
</Template>
<Template Filename="ERChild.NON.RTN.xml">
<SQLCommand>SELECT Addresses.Address_Line1, Addresses.Address_Line2,
Addresses.Address_Line3, Addresses.Address_Line4, Addresses.Postcode
FROM Addresses</SQLCommand>
</Template>
</Templates>

XmlDocument xdTemplateInfo = new XmlDocument();
xdTemplateInfo.LoadXml(strJobXml);

(strJobXml contains the Xml as above). I then would like to retrieve
the SQLCommand for a Template whose filename attribute is the same as
my search string (strSearchFile). I thought that I could do this as
follows:

XmlNodeList xlTemplates =
xdTemplateData.SelectNodes("Templates/Template");
foreach (XmlNode xnTemplate in xlTemplates)
{
if (xnTemplate.Attributes.GetNamedItem("Filename").In nerText ==
strSearchFile)
{
//retrieve the SQL
//TODO: the following statement appears to always return the first
//matching node of the document!
XmlNode xnSql =
xnTemplate.SelectSingleNode("/Templates/Template/SQLCommand");
strSQLCommand.Append(xnSql.InnerText);
break;
}
}

However, even though xnTemplate refers to a SingleNode in xlTemplates
(the list of Template nodes) the SelectSingleNode operation always
returns the first matching SQLCommand element in the XmlDocument i.e.
the SQL Command for the template with filename NON.RTN.FORMA3.xml. I
would like to know why the SelectSingleNode does not apply to the
current node but the whole document and, is there a way to select a
sub-node of a current node?

many thanks

Claire

Nov 12 '05 #3

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

Similar topics

6
7056
by: Jay Bienvenu | last post by:
I have the following Visual Basic .NET code in an ASP.NET project: ' ... Dim xdStructure As XmlDocument Dim xnStructureRoot As XmlNode xdStructure = New XmlDocument...
2
4843
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...
7
3795
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
6
7787
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
3287
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...
1
3604
by: Marja Ribbers-de Vroed | last post by:
The following is a code snippet of a classic ASP webapplication: Set l_oNode = p_oXML.selectSingleNode(l_sXPath) If (Not IsNull(l_oNode)) Then l_sValue = l_oNode.text End If According to...
5
9752
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 : ...
1
1514
by: SailBoffin | last post by:
I just noticed something confusing the MSDN documentation for the XmlNode.SelectSingleNode() function here http://msdn2.microsoft.com/en-us/library/fb63z0tw.aspx It says, under the "Return...
0
1765
by: compumate99 | last post by:
I am trying to parse the xml document using selectsinglenode method. I am doing this using Visual Foxpro >>> loResultXml = CreateObject("Microsoft.XMLDOM") With loResultXml .Async = .F. ...
1
6760
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
7022
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
5365
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
4501
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
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
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 ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.