473,607 Members | 2,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SelectSingleNod e 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.R TN.FORMA3.xml">
<SQLCommand>SEL ECT Addresses.Addre ss_Line1, Addresses.Addre ss_Line2,
Addresses.Addre ss_Line3, Addresses.Addre ss_Line4, FROM
Addresses</SQLCommand>
</Template>
<Template Filename="ERChi ld.NON.RTN.xml" >
<SQLCommand>SEL ECT Addresses.Addre ss_Line1, Addresses.Addre ss_Line2,
Addresses.Addre ss_Line3, Addresses.Addre ss_Line4, Addresses.Postc ode
FROM Addresses</SQLCommand>
</Template>
</Templates>

XmlDocument xdTemplateInfo = new XmlDocument();
xdTemplateInfo. LoadXml(strJobX ml);

(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("Te mplates/Template");
foreach (XmlNode xnTemplate in xlTemplates)
{
if (xnTemplate.Att ributes.GetName dItem("Filename ").InnerTex t ==
strSearchFile)
{
//retrieve the SQL
//TODO: the following statement appears to always return the first
//matching node of the document!
XmlNode xnSql = xnTemplate.Sele ctSingleNode("/Templates/Template/SQLCommand");
strSQLCommand.A ppend(xnSql.Inn erText);
break;
}
}

However, even though xnTemplate refers to a SingleNode in xlTemplates
(the list of Template nodes) the SelectSingleNod e 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 SelectSingleNod e 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 6478
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*********@ya hoo.co.uk> wrote in message
news:c6******** *************** **@posting.goog le.com...
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.R TN.FORMA3.xml">
<SQLCommand>SEL ECT Addresses.Addre ss_Line1, Addresses.Addre ss_Line2,
Addresses.Addre ss_Line3, Addresses.Addre ss_Line4, FROM
Addresses</SQLCommand>
</Template>
<Template Filename="ERChi ld.NON.RTN.xml" >
<SQLCommand>SEL ECT Addresses.Addre ss_Line1, Addresses.Addre ss_Line2,
Addresses.Addre ss_Line3, Addresses.Addre ss_Line4, Addresses.Postc ode
FROM Addresses</SQLCommand>
</Template>
</Templates>

XmlDocument xdTemplateInfo = new XmlDocument();
xdTemplateInfo. LoadXml(strJobX ml);

(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("Te mplates/Template");
foreach (XmlNode xnTemplate in xlTemplates)
{
if (xnTemplate.Att ributes.GetName dItem("Filename ").InnerTex t ==
strSearchFile)
{
//retrieve the SQL
//TODO: the following statement appears to always return the first
//matching node of the document!
XmlNode xnSql =
xnTemplate.Sele ctSingleNode("/Templates/Template/SQLCommand");
strSQLCommand.A ppend(xnSql.Inn erText);
break;
}
}

However, even though xnTemplate refers to a SingleNode in xlTemplates
(the list of Template nodes) the SelectSingleNod e 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 SelectSingleNod e 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******@hotma il.com> wrote in message news:<eT******* *******@TK2MSFT NGP10.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*********@ya hoo.co.uk> wrote in message
news:c6******** *************** **@posting.goog le.com...
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.R TN.FORMA3.xml">
<SQLCommand>SEL ECT Addresses.Addre ss_Line1, Addresses.Addre ss_Line2,
Addresses.Addre ss_Line3, Addresses.Addre ss_Line4, FROM
Addresses</SQLCommand>
</Template>
<Template Filename="ERChi ld.NON.RTN.xml" >
<SQLCommand>SEL ECT Addresses.Addre ss_Line1, Addresses.Addre ss_Line2,
Addresses.Addre ss_Line3, Addresses.Addre ss_Line4, Addresses.Postc ode
FROM Addresses</SQLCommand>
</Template>
</Templates>

XmlDocument xdTemplateInfo = new XmlDocument();
xdTemplateInfo. LoadXml(strJobX ml);

(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("Te mplates/Template");
foreach (XmlNode xnTemplate in xlTemplates)
{
if (xnTemplate.Att ributes.GetName dItem("Filename ").InnerTex t ==
strSearchFile)
{
//retrieve the SQL
//TODO: the following statement appears to always return the first
//matching node of the document!
XmlNode xnSql =
xnTemplate.Sele ctSingleNode("/Templates/Template/SQLCommand");
strSQLCommand.A ppend(xnSql.Inn erText);
break;
}
}

However, even though xnTemplate refers to a SingleNode in xlTemplates
(the list of Template nodes) the SelectSingleNod e 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 SelectSingleNod e 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
7067
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 xdStructure.Load(Server.MapPath("~/xml/Structure.xml"))
2
4858
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 XmlDocument(); try { string node = Request.QueryString; //getting nodeid from query string doc.Load(Session.ToString()); //load xml file from
7
3811
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", myXmlNSMgr).InnerText;
6
7800
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 statement works fine on the document in XmlSpy. However, when I call this is .NET 1.1, I get the following exception: XPathException {"Namespace Manager or XsltContext needed. This query has a
3
3298
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 UserAtHost="PBoul@team.de" Enabled="1" VersionPermission="10" VersionContact="25" DisplayName="Boullay, Peter" Email="PBoul@team.de" Phone="+88
1
3615
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 http://www.devguru.com/Technologies/xmldom/quickref/node_selectSingleNode..html the selectSingleNode method should return Null when no node was found for the XPath expression, but it doesn't. At least not in a way that satisfies the "Not IsNull()" test....
5
9762
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 : I have an XHTML document in string form which I parse using Xerces DOMParser (using an InputSource to read it). Now I am trying to get to a node for which I have the Xpath, using the XPathAPI.
1
1532
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 Value" section that "The XmlNode should not be expected to be connected "live" to the XML document." but in the sample code modifies the node returned from the function and then prints the entire document. Did I mis-interpret the description of the...
0
1796
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. .Load(pcXmlFile) oPackage = .documentElement.SelectSingleNode("//Package")
0
7985
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8469
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8463
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8322
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5997
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3953
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2461
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 we have to send another system
0
1316
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.