check if node exists
-
Monday, January 23, 2006 5:23 AM
i want to check if an element exists and manipulate this element if it does, eg...
XmlNode node = ....
if (node.selectnodes("whatever") exists)
then...
do something
thanks if you can help.
All Replies
-
Monday, January 23, 2006 6:58 AM
You can do this on several ways. You can walktrough all nodes with a XmlTextReader and look for a specific Xml Node Name.
You can XPath as well. This is a simple query language for Xml, for more information about it look here.
Here is a little example that select a specified XmlNode:
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");
//Select the book node with the matching attribute value.
XmlNode nodeToFind;
XmlElement root = doc.DocumentElement;
// Selects all the title elements that have an attribute named lang
nodeToFind = root.SelectSingleNode("//title[@lang]");
if( nodeToFind != null )
{
// It was found, manipulate it.
}
else
{
// It was not found.
}
}
}
-
Tuesday, January 24, 2006 8:17 AM
thankyou!!
does the same thing work with attributes
eg.
if(node.Attributes["attributes"] != null)
...
-
Tuesday, January 31, 2006 5:21 AMAttributes can be selected using XPath expressions via SelectNodes and SelectSingleNodes, just prefix the attribute name with the path expression '@'. See http://msdn.microsoft.com/library/en-us/xmlsdk/html/daa75346-ccd1-4033-9250-9b9f3ba5762c.asp
-
Thursday, October 01, 2009 5:08 PMThis can also be achieved through:
'' DETERMINE IF THE xml contains the older variable name or the newer correct variable name "Of" versus "of".
If locXmlNodes(0).Item("Dude") Is Nothing Then
Response.Write("Dude does not exist")
Else
Response.Write("Dude does exist")
End If
Other things I started to find helpful with xpath are as follows too:
locXmlNodes = reqXmlDoc.SelectNodes("//TraineeHistory/Person/PersonHistory/PersonTraining/Course/CourseCode[contains(.,'S') and substring-after(.,'')]/../DateofTraining[contains(.,'2009') and substring-after(.,'')]/..")
Response.Write("NUMBER OF PARENT NODES in houston data is with CourseCode='*S*' and DateofTraining='2009' is:" + locXmlNodes.Count.ToString() + "<Br>")
'Response.Write(locXmlNodes("CourseCode").Value.ToString())
Response.Write(locXmlNodes(0).InnerXml.ToString())
'Response.Write(locXmlNodes(0).Item("CourseCode").Value.ToString())
Response.Write("<br/>" + locXmlNodes(0).HasChildNodes.ToString())
Response.Write("<br/>" + locXmlNodes(0).ChildNodes.Count.ToString())
Response.Write("<br/>" + locXmlNodes(0).FirstChild.ToString())
Response.Write("<br/>" + locXmlNodes(0).FirstChild.InnerXml.ToString())
Response.Write("<br/>" + locXmlNodes(0).Item("CourseCode").InnerXml.ToString())
Response.Write("<br/>" + locXmlNodes(0).Item("CourseName").InnerXml.ToString())
Response.Write("<br/>" + locXmlNodes(0).Item("DateOfExpiration").InnerXml.ToString())
Response.Write("<br/>" + locXmlNodes(0).Item("DateofTraining").InnerXml.ToString())
Response.Write("<br/>" + locXmlNodes(0).Item("TrainingType").InnerXml.ToString())Response.Write("<br/>")
locXmlNodes = reqXmlDoc.SelectNodes("//TraineeHistory/Person/PersonHistory/PersonTraining/Course/TrainingType[.='BASIC' or .='BASICREF' or .='CONFINED']/..")
Response.Write("[c]NUMBER OF NODES in houston data is with TrainingType='BASIC' or TrainingType='BASICREF' or TraniningType='CONFINED' is:" + locXmlNodes.Count.ToString() + "<BR>")'======================================
' format of date needs to be 10/01/2009 it can not be 10/1/09; it can not be 1/1/09; it has to have 10 characters to work
' we will then do a integer comparison for the yyyymm value since you can ot compare dates in XPATH natively
' SEARCH REFERENCE: asp.net xpath compare dates in xml nodes or atributes
'======================================
strXPathQuery = "(substring(.,7,4)*100)+substring(.,1,2) >= " + todayYearMonth.ToString()
Response.Write("<br/>")
locXmlNodes = reqXmlDoc.SelectNodes("//TraineeHistory/Person/PersonHistory/PersonTraining/Course/TrainingType[.='BASIC' or .='BASICREF' or .='CONFINED']/../DateOfExpiration[" + strXPathQuery + "]/..")
Response.Write("[c]NUMBER OF NODES in houston data is with TrainingType='BASIC' or TrainingType='BASICREF' or TraniningType='CONFINED' And DateOfExpiration >= " + todayYearMonth.ToString() + " is:" + locXmlNodes.Count.ToString() + "<BR>")
If locXmlNodes.Count >= 1 Then
If locXmlNodes(0).HasChildNodes Then
For listCnt = 0 To locXmlNodes.Count - 1
Response.Write("(" + listCnt.ToString() + ")CourseCode=" + locXmlNodes(listCnt).Item("CourseCode").InnerXml.ToString() + "<BR/>")
Response.Write("(" + listCnt.ToString() + ")CourseName=" + locXmlNodes(listCnt).Item("CourseName").InnerXml.ToString() + "<BR/>")
Response.Write("(" + listCnt.ToString() + ")DateOfExpiration=" + locXmlNodes(listCnt).Item("DateOfExpiration").InnerXml.ToString() + "<BR/>")
If locXmlNodes(listCnt).Item("DateofTraining") Is Nothing Then
Response.Write("(" + listCnt.ToString() + ")***DateOfTraining=" + locXmlNodes(listCnt).Item("DateOfTraining").InnerXml.ToString() + "<BR/>")
Else
Response.Write("(" + listCnt.ToString() + ")DateofTraining=" + locXmlNodes(listCnt).Item("DateofTraining").InnerXml.ToString() + "<BR/>")
End If
If Not (locXmlNodes(0).Item("TrainingType") Is Nothing) Then
Response.Write("(" + listCnt.ToString() + ")TrainingType=" + locXmlNodes(listCnt).Item("TrainingType").InnerXml.ToString() + "<BR/>")
End If
Response.Write("========== SAVE TO THE DATABASE")
Response.Write("<br>")
Next
End If
End If
GREATE WEBSITE REFERENCES I FOUND WERE AS FOLLOWS:
http://bytes.com/topic/visual-basic-net/answers/372556-xpath-query-multiple-parameters
http://www.eggheadcafe.com/articles/20030627d.asp
asp.net best way to query and save xml data to sql database
http://stackoverflow.com/questions/61233/the-best-way-to-shred-xml-data-into-sql-server-database-columns
http://stackoverflow.com/questions/667168/best-method-to-populate-xml-from-sql-query-in-asp-net
search:asp.net xpath compared to linqtoxml (SOUNDS TO ME XPATH STILL IS A GOOD METHOD FOR QUERyiNG XML DATA)
http://smartdev.wordpress.com/2009/04/15/xpath-vs-linq-to-xml/
http://blog.dreamlabsolutions.com/post/2008/12/04/LINQ-to-XML-and-LINQ-to-XML-with-XPath-performance-review.aspxsearch:
http://forums.asp.net/p/1072271/1569035.aspx
http://forums.asp.net/t/1123338.aspx
http://forums.asp.net/p/1399792/3023829.aspx
IMPORTANT THOUGHT ....WE HAVE TO GET THE PARENT
http://bytes.com/topic/visual-basic-net/answers/372556-xpath-query-multiple-parameters
Thanks,
Doug Lubey of Louisiana
www.douglubey.com- Edited by Doug Lubey of Louisiana Thursday, October 01, 2009 5:31 PM problem with code
-
Tuesday, May 17, 2011 12:01 PM
Hi
SelectSingleNode is not working for me in VBScript. if the node does not exist.
Set objFamAttrXML = CreateObject("Microsoft.XMLDOM")
objFamAttrXML.Async = "false"
objFamAttrXML.SetProperty "SelectionLanguage", "XPath"
objFamAttrXML.validateOnParse = False
objFamAttrXML.resolveExternals = False
objFamAttrXML.Load (MY_QC & "\\Family_Attr.XML")
logW ADD,DBG, "XML File : " & MY_QC & "\Family_Attr.XML"
set famNode = Nothing
famNode = objFamAttrXML.SelectSingleNode("/familyattr/family[@attr='" & CIFamily & "']")
If Not IsObject(famNode) then
logW ADD, ERROR, "Family does not exist in " & MY_QC & "\Family_Attr.XML"
bPASS = false
myRC = -1
Exit Sub
End if
The If is never executed and script breaks after selectsinglenode statment.
Anubhav -
Tuesday, May 17, 2011 12:21 PM
Consider to start a new thread with your own topic, then please provide a sample of the XML you are running that VBScript code against and tell us exactly which error message you get. If you want us to understand your problem then it is best to post enough code to allow us to easily reproduce the problem so show all variable declarations and initializations.
In general I would suggest the following code changes:
- Instead of objFamAttrXML.Async = "false" use objFamAttrXML.Async = False
- Check the result of calling the load method e.g.
If objFamAttrXML.Load (MY_QC & "\\Family_Attr.XML") Then
' now here you can access nodes
Else
'check or output objFamAttrXML.parseError.reason
End If
I also think instead of
If Not IsObject(famNode) then
You rather need
If famNode Is Nothing Then
MVP Data Platform Development My blog

