Hey SonaliB,
Please refer below sample.
Namespaces
C#
using System.Xml;
using System.Globalization;
using System.Text.RegularExpressions;
VB.Net
Imports System.Xml
Imports System.Globalization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Regex regex = new Regex(@"(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$");
string[] formats = { "yyyy-MM-dd" };
string FilePath = Server.MapPath("~/XMLFile.xml");
XmlDocument doc = new XmlDocument();
doc.Load(FilePath);
XmlNodeList nodeList = doc.SelectNodes("/Record/record/AirDate");
foreach (XmlNode node in nodeList)
{
bool isValid = regex.IsMatch(node.InnerText);
if (!isValid)
{
string datetime = node.InnerText;
if (string.IsNullOrEmpty(datetime))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('xml file have no date.');", true);
}
else
{
DateTime yyydate = Convert.ToDateTime(datetime);
string result = yyydate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim regex As Regex = New Regex("(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$")
Dim formats As String() = {"yyyy-MM-dd"}
Dim FilePath As String = Server.MapPath("~/XMLFile.xml")
Dim doc As XmlDocument = New XmlDocument()
doc.Load(FilePath)
Dim nodeList As XmlNodeList = doc.SelectNodes("/Record/record/AirDate")
For Each node As XmlNode In nodeList
Dim isValid As Boolean = regex.IsMatch(node.InnerText)
If Not isValid Then
Dim datetime As String = node.InnerText
If String.IsNullOrEmpty(datetime) Then
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "CropImage", "alert('xml file have no date.');", True)
Else
Dim yyydate As DateTime = Convert.ToDateTime(datetime)
Dim result As String = yyydate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
End If
End If
Next
End If
End Sub