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.
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;
}