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.

How to check whether element value at any XPath is alphanumeric or not.<BR>
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;
}

}
}


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: