dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » General

How to check whether user defined date format matches with element value’s date format at XPath?


Posted Date:     Category: General    
Author: Member Level: Gold    Points: 15


There is a sample code to check whether any element value’s datetime format matches with the format given by user in XPath in xml file or not. Code has been developed in C# whereas application is windows based.



 


Suppose user wants to give different date time formats such as "MM-DD-YYYY" or "DD-MM-YYYY" or any other formats.
Formats of an element values needs to be checked against the datetime formats. If it matches with user defined date-time format then it will return as true else it is false. Make sure that only element values present at XPath will get considered. It will not check with all element value formats.

public bool match_elementval_with_given_date_format(string user_given_xpath, string user_defined_xml, string expected_date_format)
{
bool result = false;
XmlDocument user_defined_xml_document = new XmlDocument();
user_defined_xml_document.Load(user_defined_xml);
XmlNodeList myNodeList = user_defined_xml_document.SelectNodes(user_given_xpath);
foreach (XmlNode myNode in myNodeList)
{
bool get_result = date_format_validate (myNode.InnerText.Trim() , expected_date_format);
if (get_result == true)
{
result = true;
return result;
}
else
{
result = false;

}
}

return result;
}

public bool date_format_validate(string date, string date_format)
{
try
{
DateTime.ParseExact(date, date_format, DateTimeFormatInfo.InvariantInfo);
}
catch
{
return false;
}
return true;
}





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


Responses to "How to check whether user defined date format matches with element value’s date format at XPath?"

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 an element is present in the expected range at xpath in XML file?
    Previous Resource: How to create XML file with dynamic data using C#?
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    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.