Tuesday, March 27, 2012

C# CODE TO CONVERT CSV FILE TO XML FILE FORMAT

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace textToXml
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(@"C:\Documents and Settings\Shalu\Desktop\mywork\Dictionary");
            StreamWriter sw = new StreamWriter(@"d:\Dictionary1.xml");
              
            try
            {
                string table = "Table";
                string TableName = "TableName";
                string FirstLine;
                string line;
                string[] col;
                string[] values;
                FirstLine = sr.ReadLine();
                col = FirstLine.Split('\t');

                sw.Write(OpenTag(table));


                while ((line = sr.ReadLine()) != null)
                {
                    sw.Write(OpenTag(TableName));
                    values = line.Split(',');
                    for (int i = 0; i < values.Length; i++)
                    {
                        col[i] = col[i].Replace(" ", "_x0020_");
                        if (values[i] == "")
                        {
                            string t = Tag(col[i], "NULL");
                            sw.Write(t);
                        }
                        if (values[i] == "&")
                        {
                            string t = Tag(col[i], "&amp;");
                            sw.Write(t);
                        }
                        if (values[i] == "<")
                        {
                            string t = Tag(col[i], "&lt;");
                            sw.Write(t);
                        }
                        if (values[i] == ">")
                        {
                            string t = Tag(col[i], "&gt;");
                            sw.Write(t);
                        }
                        if (values[i] == "\"")
                        {
                            string t = Tag(col[i], "&quot;");
                            sw.Write(t);
                        }
                        if (values[i] == "\'")
                        {
                            string t = Tag(col[i], "&apos;");
                            sw.Write(t);
                        }
                        else
                        {
                            string t = Tag(col[i], values[i]);
                            sw.Write(t);
                        }
                       
                    }
                    sw.Write(CloseTag(TableName));
                }

               
                sw.Write(CloseTag(table));
            }
            finally
            {
                sr.Close();
                sw.Close();
            }
           
        }
        public static string OpenTag(string col)
        {
            return "<"+col+">";
        }
        public static string CloseTag(string col)
        {
            return (@"</") + col + ">";
        }
        public static string Tag(string columnName, string columnValue)
        {
            return OpenTag(columnName) + columnValue + CloseTag(columnName);
        }
    }
}

No comments:

Post a Comment