Connecting Tech Pros Worldwide Forums | Help | Site Map

SelectSingleNode always selects same node when looping through

Claire Reed
Guest
 
Posts: n/a
#1: Nov 12 '05
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

Jerry Pisk
Guest
 
Posts: n/a
#2: Nov 12 '05

re: SelectSingleNode always selects same node when looping through


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" <clairereedo@yahoo.co.uk> wrote in message
news:c64fb943.0410110614.c8bc87c@posting.google.co m...[color=blue]
> 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[/color]


Claire Reed
Guest
 
Posts: n/a
#3: Nov 12 '05

re: SelectSingleNode always selects same node when looping through


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" <jerryiii@hotmail.com> wrote in message news:<eTD3gzBsEHA.3076@TK2MSFTNGP10.phx.gbl>...[color=blue]
> 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" <clairereedo@yahoo.co.uk> wrote in message
> news:c64fb943.0410110614.c8bc87c@posting.google.co m...[color=green]
> > 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[/color][/color]
Closed Thread