How to check whether expected element value is present in XPath in user defined XML file.


There is a sample code to check whether the element is present in XPath in xml file or not. User can set xpath , expected element value and xml file dynamically to check this. Code has been developed in C# whereas application is windows based.

How to check whether expected element value is present in XPath in user defined XML file
Description -
Create a class library which will check whether the element value is present in XPath in xml or not. Suppose expected element value is "student1" in xpath "//StudentsList/student" in test.xml then following code will be used for this -


public bool userdefined_element_value(string userdefined_expectedval , string userdefined_xml_filepath , string userdefined_xpath )
{
bool check_result = false;

XmlDocument user_xml_document = new XmlDocument();
user_xml_document.Load(userdefined_xml_filepath);
XmlNodeList mynodeList = user_xml_document.SelectNodes(userdefined_xpath);
foreach (XmlNode mynode in mynodeList)
{
if (mynode.InnerText.Equals(userdefined_expectedval))
{
check_result = true;
return check_result;
}
else
{
check_result = false;
}
}

return check_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: