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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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",...
|
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...
|
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...
| |
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...
|
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 :
...
|
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...
|
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.
...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |