0

In my project I need to read a csv file and convert it into xml and save the xml to database table. I want to save the xml Output directly to database without saving to a file. I was able to convert csv to xml, but I am not sure how to save it(without saving to a file) database directly. Any help is appreciated.

Here is my code

   var lines = System.IO.File.ReadAllLines(@"C:\test.csv");

            var xml = new XElement("TopElement",
               lines.Select(line => new XElement("Item",
                line.Split(';')
                .Select((column, index) => new XElement("Column" + index, column)))));

           // XmlTextReader reader = new XmlTextReader(xml.ToString());


            //xml.Save(@"C:\xmloutput.xml);  // dont want to save to file.
7
  • What kind of database are you using? You may want to do some research on ADO.NET Commented Sep 13, 2012 at 14:36
  • use a memorystream? and a binary field in your database. example Commented Sep 13, 2012 at 14:36
  • Why not save the file as binary to databse why do you think it better the other way it a lot faster to save the file directly unless, you need to extract the data from, it Commented Sep 13, 2012 at 14:37
  • 1
    Why from excel file to xml and then to database? Why not directly to database? Commented Sep 13, 2012 at 14:38
  • You have the XML you want in the xml variable. Can't you just insert that into the database directly? Commented Sep 13, 2012 at 14:39

3 Answers 3

2

Do you have column of type "XMl" in database in case you are using SQL Server?

You can check Save XML directly to Database with C#

Sign up to request clarification or add additional context in comments.

3 Comments

I want to insert the entire data into one single row , In for later use, In such case above solutions doesn't work
Your solution is right. I was able to insert xml into table like any other data. Thanks for help
@Henry if the solution is correct then please UP vote as well.
0

Maintaining XML in SQL Server:

http://msdn.microsoft.com/en-us/library/bb510480(SQL.105).aspx

C# sample (converting XML to string): http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/d1666d13-dea3-4ce8-a818-6b852a63de4f/

Comments

0

Implementing XML in SQL Server:

http://msdn.microsoft.com/en-us/library/ms189887(SQL.105).aspx

Other links:

http://forums.asp.net/t/1316853.aspx/1

2 Comments

I want to insert the entire data into one single row , In for later use, In such case above solutions doesn't work
You can make the whole xml as string or serialize and save it in single row. That is one of resolutions provided above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.