-1

I am writing a web application in C#.NET MVC 4. I have a web form which inserts data to an XML file within the APP_Data folder. I am wondering next if i can load this data into a sql server database and how i would go about doing this!

eoinDeveloper
  • 103
  • 1
  • 3
  • 12
  • SQLServer has built-in functions to output any result out to XML format but I am not sure about how easy it is to do it the other way around and insert-from a XML file. – djangofan Jan 10 '14 at 01:07
  • possible duplicate : [Importing data from XML file to SQL database](http://stackoverflow.com/questions/10795787/importing-data-from-xml-file-to-sql-database) – har07 Jan 10 '14 at 01:19

1 Answers1

0

Super easy. I do something similar where I have a table with an Identity primary key column, a Created Date DateTime column, and a Data column that's type XML. You can insert the XML from .Net as a string. Instead of going through the labor of creating an XmlDocument, I just have strongly typed Model and serialize it to a string.

string xmlData;
var xmlSerializer = new XmlSerializer(model.GetType());

using (var sw = new StringWriter()) {
    xmlSerializer.Serialize(sw, model);
    xmlData = sw.ToString();
}

using (var efContext = new MyDbEntities()) {
    evContext.Documents.Add(new Document() {
        CreatedDate = DateTime.Now,
        Data = xmlData
    });
    efContext.SaveChanges();
}
Daniel Gimenez
  • 18,530
  • 3
  • 50
  • 70