Read XML file and check Date is Null or Not using C# and VB.Net in ASP.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago


this is the code I am uploading xml file, In that xml file there is date after uploading it will check date in xml file if date will be in yyyy/mm/dd format it will convert it into dd/mm/yyyy format, But sometimes I want to pass null date how can it proceed?? give me suggession on this how to check date is null in 

                Dim mystr As String = String.Concat(serverpath, FileName)
                Dim document As XDocument = XDocument.Load(mystr)
                Dim xmlFile As XmlReader
                Dim ds As New DataSet
                xmlFile = XmlReader.Create(mystr, New XmlReaderSettings())
                ds.DataSetName = "rovi"

                Dim result As String
                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 i As Integer
                i = 0
                For Each dr As DataRow In ds.Tables(0).Rows

                    Dim isValid As Boolean = regex.IsMatch(dr.Item(2))
                    If Not isValid Then
                        Dim yyydate As DateTime = Convert.ToDateTime(dr.Item(2))
                        ds.Tables(0).Rows(i).Item(2) = yyydate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                    End If
                    i = i + 1


                Dim dt As DataTable = ds.Tables(0)
                Validate(dt, rdformat)

This is xml code: 

<?xml version='1.0' encoding='UTF-8'?>
<Program_Title>The First</Program_Title>


You are viewing reply posted by: pandeyism 7 months ago.
Posted 7 months ago

Hey SonaliB,

Please refer below sample.



using System.Xml;
using System.Globalization;
using System.Text.RegularExpressions;


Imports System.Xml
Imports System.Globalization



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();
        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);
                    DateTime yyydate = Convert.ToDateTime(datetime);
                    string result = yyydate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);


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()
        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)
                    Dim yyydate As DateTime = Convert.ToDateTime(datetime)
                    Dim result As String = yyydate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                End If
            End If
    End If
End Sub