Sunday, 5 January 2014

Convert Xml in to CSV in c#


In this article we are going to see how to convert the xml in to CSV format, based on the custom delimiters.

Files:

Source code for Convert Xml in to CSV:

  class XmlToCsv
    {
        public enum RowDelimit
        {
            Default,
            NewLine,
            Space,
            Ambescent,
            Dollar
        }
        public enum ColumnDelimit
        {
            Default,
            Comma,
            TabSpace,
            Percentage,
            OrSymbol,
            Dot
        }
        public enum DataArrange
        {
            Element,
            Attribute
        }

        public static RowDelimit RowDelimiter { set; get; }

        public static ColumnDelimit ColumnDelimiter { set; get; }

        public static void FetchRowSeparater(RowDelimit delimit, out string separater)
        {
            switch (delimit)
            {
                case RowDelimit.NewLine:
                case RowDelimit.Default:
                    separater = Environment.NewLine;
                    break;
                case RowDelimit.Space:
                    separater = " ";
                    break;
                case RowDelimit.Dollar:
                    separater = "$";
                    break;
                case RowDelimit.Ambescent:
                    separater = "&";
                    break;
                default:
                    separater = Environment.NewLine;
                    break;
            }
        }


        public static void FetchColumnSeparater(ColumnDelimit delimit, out string separater)
        {
            switch (delimit)
            {
                case ColumnDelimit.Comma:
                case ColumnDelimit.Default:
                    separater = ",";
                    break;
                case ColumnDelimit.Dot:
                    separater = ".";
                    break;
                case ColumnDelimit.OrSymbol:
                    separater = "|";
                    break;
                case ColumnDelimit.Percentage:
                    separater = "%";
                    break;
                case ColumnDelimit.TabSpace:
                    separater = "\t";
                    break;
                default:
                    separater = ",";
                    break;
            }
        }

        public static void Convert(string xmlfilepath, string csvpath, string datatag, DataArrange arrange, RowDelimit rdelimit, ColumnDelimit cdelimit)
        {
            try
            {
                StringBuilder builder = new StringBuilder();

                XDocument doc = XDocument.Load(xmlfilepath);
                string Rowseparater = string.Empty;
                FetchRowSeparater(rdelimit, out Rowseparater);

                string Columnseparater = string.Empty;
                FetchColumnSeparater(cdelimit, out Columnseparater);

                foreach (XElement data in doc.Descendants(datatag))
                {
                    if (arrange == DataArrange.Element)
                    {
                        foreach (XElement innnerval in data.Elements())
                        {
                            builder.Append(innnerval.Value);
                            builder.Append(Columnseparater);
                        }
                    }
                    else
                    {
                        foreach (XAttribute innerval in data.Attributes())
                        {
                            builder.Append(innerval.Value);
                            builder.Append(Columnseparater);
                        }
                    }
                    builder.Append(Rowseparater);
                }

                File.AppendAllText(csvpath, builder.ToString());
            }
            catch
            {
                throw;
            }
        }
    }



Input Xml File:

<Employees>
<Employee name="Rajesh" address="Chennai" dept="IT" />
<Employee name="Suresh" address="Chennai" dept="CSE" />
<Employee name="Siva" address="US" dept="R&amp;D" />
</Employees>





Read the xml which have values in Attributes and convert that into CSV.

          try
            {
                XmlToCsv.Convert(@"D:\data\attrxml.xml", @"D:\data\rowxml.csv", "Employee", XmlToCsv.DataArrange.Attribute, XmlToCsv.RowDelimit.NewLine, XmlToCsv.ColumnDelimit.Comma);
                Console.Write("Done");
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            Console.Read();

Read the xml which have values in Elements and convert that into CSV.

Input Xml File:

<Employees>
<Employee>
    <name>Rajesh</name>
    <address>Chennai</address>
    <dept>IT</dept>
</Employee>
<Employee>
    <name>Suresh</name>
    <address>Chennai</address>
    <dept>CSE</dept>
</Employee>
<Employee>
    <name>Siva</name>
    <address>US</address>
    <dept>R&amp;D</dept>
</Employee>
</Employees>


            try
            {
                XmlToCsv.Convert(@"D:\data\elementxml.xml", @"D:\data\elemntxml.csv", "Employee", XmlToCsv.DataArrange.Element, XmlToCsv.RowDelimit.NewLine, XmlToCsv.ColumnDelimit.Comma);
                Console.Write("Done");
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            Console.Read();


Output:

Rajesh,Chennai,IT,
Suresh,Chennai,CSE,
Siva,US,R&D,


You can change the delimiters based on your requirements.


From this post you can understand how to implement how to xml to csv format.

No comments:

Post a Comment