SSK
SSK

Reputation: 803

Reading the xml file in server without saving it

I am uploading an xml file in asp.net. what i want to do is to read the file and convert it to xmldoc and send it to one webservice without saving the xml file in the server. Is it possible? If yes can anyone help me with the code. The code i wrote so far is as below

HttpPostedFile myFile = filMyFile.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen > 0)
{
byte[] myData = new byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
}

Upvotes: 2

Views: 5759

Answers (2)

Anthony Pegram
Anthony Pegram

Reputation: 126932

Using ASP.NET's FileUpload control <asp:FileUpload>, you can load the uploaded file like this. Shows loading an XmlDocument and an XDocument.

using (MemoryStream stream = new MemoryStream(fileUpload.FileBytes))
{
    XmlDocument document = new XmlDocument();
    document.Load(stream);

    stream.Position = 0; // return to beginning for demo
    XDocument xdocument = XDocument.Load(XmlReader.Create(stream));
}

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630579

You can load it up using the input stream of the posted file, like this:

XmlDocument doc = new XmlDocument();
doc.Load(myFile.InputStream);

This uses the .Load(Stream) overload of XmlDocument.

Upvotes: 7

Related Questions