Baper
Baper

Reputation: 1543

Saving an XML file using Fileupload

How can I save the content of an xml file to a database( in field which has an XML type)

should I read the content of file with i.e:

FileUpload1.FileContent;

and then send it as a parameter to save in Database? Is it correct?

Upvotes: 0

Views: 1377

Answers (3)

Shyju
Shyju

Reputation: 218732

You have to first save it to the Server hard disk and then get the InnerXML of that to a string variable and then save it to the database.

Assuming you saved the file to some folder in your disk, you can use the below method (using LINQtoXML) to read the content

XElement elm = XElement.Load(Server.MapPath(@"../YourUploadFolder/yourXMl.xml"));
if(elm!=null)
{
    var reader = elm.CreateReader();
    reader.MoveToContent();
    string xmlContent = reader.ReadInnerXml(); xmlContent
   // Now save to the database    
}

Upvotes: 1

Angshuman Agarwal
Angshuman Agarwal

Reputation: 4866

You can basically fill columns of type XML from an XML literal string, so you can easily just use a normal INSERT statement and fill the XML contents into that field. For this, you have to read the XML file.

You can use the System.Xml.Linq namespace and use XDocument.Load(@"YourXmlFile.xml"); or any standard method to read the XML file as described here - http://support.microsoft.com/kb/307548

Upvotes: 0

Jason De Oliveira
Jason De Oliveira

Reputation: 1742

You should use the the XmlReader and XmlTextReader classes to load the XML file into memory. They are defined in the System.XML namespace. You could also use the XDocument class defined in the System.Xml.Linq namespace. For more information please look here :

http://www.c-sharpcorner.com/uploadfile/mahesh/readingxmlfile11142005002137am/readingxmlfile.aspx

http://support.microsoft.com/kb/307548

var reader = new XmlTextReader("C:\\temp\\xmltest.xml");

You then store the XML content as XML in the DB if possible (depending on the DB system you use) or as varchar. Would be better to store them as XML though since you may then assure that its is well-formatted and validated against a certain schema for example !

Upvotes: 1

Related Questions