How to check whether a character is present in the value of given XPath of XML file?
There is a sample code to check whether any particular character is present in XPath in xml file or not. User can set xpath , expected character and xml file dynamically to check this. Code has been developed in C# whereas application is windows based.
Introduction
I have developed the method which will give you true as a result, if expected character is present in the XPath of XML file. If that character is not present in xpath of xml file then it will give false as a result.
User has to give parameters as xpathvalue , xml file path , expected character in the method.
Description
Create a class library in which put this code snippet and call whenever it is required.
public bool IsCharacterPresent(string xpathvalue, string user_defined_xml_file , string expected_Character)
{
bool result_value = false;
try
{
XmlDocument user_defined_xml_document = new XmlDocument();
user_defined_xml_document.Load(user_defined_xml_file);
XmlNodeList mynodeValueList = user_defined_xml_document.SelectNodes(xpathvalue);
foreach (XmlNode mynodeValue in mynodeValueList)
{
if (mynodeValue.InnerText.Contains(expected_Character))
{
result_value = true;
return result_value;
}
else
{
result_value = false;
}
}
}
catch (Exception)
{
result_value = false;
}
return result_value;
}
Instead of a character, suppose user wants to put substring then also this method can be used.