Subscribe to Subscribers
Talk to Webmaster Tony John

Resources » .NET programming » XML

How to use regular expression validation for value at user defined XPAth in XML file?


Posted Date:     Category: XML    
Author: Member Level: Gold    Points: 25


If you want to check value at a particular XPath matches with regular expression provided by you , then you can use following code snippet. Code has been developed in C#. You can place this code in any .cs file and use the same.



 


How to use regular expression validation for value at user defined XPAth in XML file?
Introduction
User has to give input as XPath, regular expression and XML file which he wants to use for this match checking purpose.
Instead of using several methods for alphanumeric ,numeric , date checking regular expressions , user has to customize all in one single method.
Suppose your XML file is test1.xml and XPath in test1.xml is //students/student/First-name then user has to also specify which type of regular expression he wants to check against this mentioned XPath.
Lets say , user wants to apply alphanumeric regular expression then , he has to send input ^[a-zA-Z0-9]*$ for the parameter used for regular expression.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace yourprojectnamespace.subfoldername
{
class DoCheckRegularExpression
{
public bool chkregularex(string User_defined_XPath, string User_defined_Xml_file, string User_defined_regularexpression)
{
bool val = false;

try
{

XmlDocument xml_document = new XmlDocument();
xml_document.Load(User_defined_Xml_file);
XmlNodeList nodelist = xml_document.SelectNodes(User_defined_XPath);
foreach (XmlNode node in nodelist)
{
Regex pattern = new Regex(User_defined_regularexpression);
string[] outcome_reg_exp = node.InnerText.Trim().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in outcome_reg_exp)
{
if (pattern.IsMatch(s))
{
val = true;
return val;
}
else
{
val = false;
}
}
}


}
catch (XmlException)
{
System.Windows.Forms.MessageBox.Show("Please check XML file format" ,"XML file format exception");

}
return val;
}
}
}






Did you like this resource? Share it with your friends and show your love!


Responses to "How to use regular expression validation for value at user defined XPAth in XML file?"

No responses found. Be the first to respond...

Feedbacks      

Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to check whether element value at any XPath is alphanumeric or not.
    Previous Resource: Creating, reading and appending the XML file.
    Return to Resources
    Post New Resource
    Category: XML


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    XML  .  XPATH  .  
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.