0

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?

3 Answers 3

1

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    
}
Sign up to request clarification or add additional context in comments.

Comments

1

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 !

Comments

0

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

Comments

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.