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.

How to check whether an element is present in the expected range at xpath in XML file?
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.
Description -
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();
user_defined_xml_document.Load(user_defined_xml_file);
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)
{
if (myNode.InnerText.Equals(word))
{
result = true;
return result;
}
else
{
result = false;

}
}
}

return result;
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: