How to check whether element value at any XPath is alphanumeric or not.
This class library will help user to find the element value is alphanumeric or not.
User has to mention the XML file and XPath. Regular expression is used to check alphanumeric values. Result will be returned as true or false.
You have to create a class library and place following code in to it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
namespace yournamespace.subfoldername
{
class DoCheckAlphanueric
{
public bool chkelementvalueaphanumeric(string yourXMLfile, string yourXPathValue)
{
bool result = false;
XmlDocument doc = new XmlDocument();
doc.Load(yourXMLfile);
XmlNodeList myNodeList = doc.SelectNodes(yourXPathValue);
foreach (XmlNode myNode in myNodeList)
{
Regex pattern = new Regex("^[a-zA-Z0-9]*$");
string[] result = myNode.InnerText.Trim().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
if (pattern.IsMatch(s))
{
result = true;
return result;
}
else
{
result = false;
}
}
}
return result;
}
}
}