How to check whether an element is present in the expected range at xpath in XML file?
There is a sample code to check whether any element comes under expected range given by user in XPath in xml file or not. Code has been developed in C# whereas application is windows based. Scenario has been explained for more clarity.
Scenario is as follows
suppose my expected range is Science,Computer,Mathematics and I want to check whether element value comes in that particular range then this code snippet is used.
So in such a case if element is Science,Computer or Mathematics (any one out of these three) then result will be true else it will be false. User has to mention the xpath value which should be present in XML file.
This method checks the elements only at that particular xpath and not whole xml file.
User has to give parameters as xpathvalue , xml file path , expected range value (should be seperated by commas) in the method.
Create a class library in which put this code snippet and call whenever it is required. Make sure that if the element itself has comma inside it then there is no escape sequence for the same.
public bool Element_Present_In_Range(string element_range_value, string user_defined_xml_file, string user_defined_xpath_value)
bool result = false;
XmlDocument user_defined_xml_document = new XmlDocument();
XmlNodeList myNodeList = user_defined_xml_document.SelectNodes(user_defined_xpath_value);
foreach (XmlNode myNode in myNodeList)
string words = element_range_value.Split(',');
foreach (string word in words)
result = true;
result = false;