Generate XML from C#.

Hi all, in this post I’ll explain how to generate XML file using C# classes. Suppose we want to generate a XML file for our DataSet or DataTable,  what we generally do, we use “GetXml()” method of DataSet to generate XML. But in this approach we do not have one single customization option like I want nested tags, attributes etc. So GetXml() does not help us always.

Here is my approach to generate XML with full of customization features. You are free to make any changes.  The following is my data:

[EmpId]    [EmpName]             [EmpSalary]    [Status]    [Deduction]        [Notes]
1                   Sunil Kumar            60000.00        1                  5500.00              hi this is sunil
2                   Sanjeev Kumar      50000.00        1                  6000.00              NULL
3                   Poonam Sharma   35000.00        0                  2600.00              hi this is poonam and my id is 3
4                   Maya                          50000.00       1                    4000.00            hi this is maya and my id is 4

I want EmpName, EmpSalary, Deduction and Notes as element tags and EmpId as attribute of EmpName and Status(1: Active, 0:Inactive )  as attribute of Notes.

DataTable dt = DLL.GetEmployeeInfo(0);
        if (dt != null && dt.Rows.Count > 0)
        {
            if (System.IO.File.Exists(Server.MapPath(“~/UserData/DatatableXML.xml”)))
            {
                System.IO.File.Delete(Server.MapPath(“~/UserData/DatatableXML.xml”));
            }
            using (XmlWriter write = XmlWriter.Create(Server.MapPath(“~/UserData/DatatableXML.xml”)))
            {
                write.WriteStartDocument();
                write.WriteStartElement(“Employee”);
                foreach (DataRow row in dt.Rows)
                {
                    write.WriteStartElement(“EmployeeInfo”);

                    write.WriteStartElement(“Name”);
                    write.WriteAttributeString(“ID”, Convert.ToString(row[“EmpId”]));
                    write.WriteString(Convert.ToString(row[“EmpName”]));
                    write.WriteEndElement();

                    write.WriteStartElement(“Salary”);
                    write.WriteString(Convert.ToString(row[“EmpSalary”]));
                    write.WriteEndElement();

                    write.WriteStartElement(“Deduction”);
                    write.WriteString(Convert.ToString(row[“Deduction”]));
                    write.WriteEndElement();

                    write.WriteStartElement(“Notes”);
                    write.WriteAttributeString(“Status”, Convert.ToString(row[“Status”]) == “1” ? “Active” : “In-Active”);
                    write.WriteString(Convert.ToString(row[“Notes”]));
                    write.WriteEndElement();
                    write.WriteEndElement();
                }
                write.WriteEndElement();
                write.WriteEndDocument();
            }
        }
        System.Diagnostics.Process.Start(Server.MapPath(“~/UserData/DatatableXML.xml”));

1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s