Friday 20 December 2013

C# Tips : Validate or Verify XML File Data using LINQ and REGEX with C#.Net and VB.Net example

You can validate the data of XML file and identify XML elements which are failed to validate. We can easily achieve this using LINQ.

Here is example for this.
In this example we take one 'customer.xml' file. In this file we have 'name' and 'phone' element. We need to validate phone number with REGEX and display it's validate result either it is true or false.


C#. Net Example :
    static XElement CheckPhoneNumber(string phone)
    {
        Regex regex = new Regex("([0-9]{3}-)|('('[0-9]{3}')')[0-9]{3}-[0-9]{4}");
        return new XElement("isValidPhone", regex.IsMatch(phone));
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        XDocument doc = XDocument.Load((Server.MapPath("customer.xml")));
            
        var query =
            from customer in doc.Descendants("customer")
            select
                new XElement("customer",
                             customer.Descendants("name").First(),
                             customer.Descendants("Phone").First(),
                             CheckPhoneNumber((string)customer.Descendants("Phone").First()));

        foreach (var objElement in query)
        {
            Response.Write("<b>Customer Name : </b>" + objElement.Element("name").Value);
            Response.Write("<br/>");
            Response.Write("<b>Phone Number: </b>" + objElement.Element("Phone").Value);
            Response.Write("<br/>");
            Response.Write("<b>Is Phone Number Valid: </b>" + objElement.Element("isValidPhone").Value);
            Response.Write("<br/>");
            Response.Write("<br/>");
        }
        
    }

VB.Net Examples :
    Private Shared Function CheckPhoneNumber(phone As String) As XElement
        Dim regex As New Regex("([0-9]{3}-)|('('[0-9]{3}')')[0-9]{3}-[0-9]{4}")
        Return New XElement("isValidPhone", regex.IsMatch(phone))
    End Function


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim doc As XDocument = XDocument.Load((Server.MapPath("customer.xml")))

        Dim query = From customer In doc.Descendants("customer")
                    Select New XElement(
                                            "customer",
                                            customer.Descendants("name").First(),
                                            customer.Descendants("Phone").First(),
                                            CheckPhoneNumber(customer.Descendants("Phone").First())
                                        )



        For Each objElement As XElement In query
            Response.Write("<b>Customer Name : </b>" + objElement.Element("name").Value)
            Response.Write("<br/>")
            Response.Write("<b>Phone Number: </b>" + objElement.Element("Phone").Value)
            Response.Write("<br/>")
            Response.Write("<b>Is Phone Number Valid: </b>" + objElement.Element("isValidPhone").Value)
            Response.Write("<br/>")
            Response.Write("<br/>")
        Next
    End Sub

customer.xml File :
<?xml version="1.0" encoding="utf-8" ?>
<customers>
    <customer id="1">
        <name>Rahul</name>
        <Phone>030-0074121</Phone>
    </customer>
    <customer id="2">
        <name>Sachin</name>
        <Phone>030-0074221</Phone>
    </customer>
    <customer id="3">
        <name>David</name>
        <Phone>abc</Phone>
    </customer>
</customers>

Output : 


Below are the books that you would like :

No comments:

Post a Comment