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

Last Reply one month ago By pandeyism

Posted one month ago

Hi

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 vb.net 

                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.ReadXml(xmlFile)
                ds.DataSetName = "rovi"

                'NewCode_03/12/2018'
                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

                Next

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

This is xml code: 

<?xml version='1.0' encoding='UTF-8'?>
<record>
<record>
<CH_Code></CH_Code>
<Channel></Channel>
<AirDate></AirDate>
<Start></Start>
<Duration></Duration>
<Program_Title>The First</Program_Title>
<Original_Title></Original_Title>
<Aka_Title></Aka_Title>
<Episode_Title></Episode_Title>
<Ori_Episode_Title></Ori_Episode_Title>
<Episode_Number></Episode_Number>
<Channel_Country></Channel_Country>
<Synopsis></Synopsis>
<Actors></Actors>
<Director></Director>
<Presenter></Presenter>
<Guests></Guests>
<Production></Production>
<Distrib></Distrib>
<Country></Country>
<Product_Year></Product_Year>
<Chn_Category></Chn_Category>
<Language></Language>
<Ori_Language></Ori_Language>
<Category></Category>
<Type></Type>
<Content></Content>
<Event_ID></Event_ID>
<Program_ID></Program_ID>
<Emission_Duration></Emission_Duration>
<Season_Number></Season_Number>
<Series_ID></Series_ID>
<Part_Number></Part_Number>
<Tot_Nb_Parts></Tot_Nb_Parts>
<Live/>
<Pilot/>
<First_Showing/>
<Last_Showing/>
<Subtitled/>
</record>
</record>

 

Posted one month ago

I will get back soon.


Posted one month ago

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