tags:

views:

74

answers:

2

Hello,

Apologies for what might be a simple question; I'm getting back into coding after a seven year absence. Loving it, but everything is taking me so long!

Anyway, I'm trying to upload a file from the browser and then read it into an XmlDocument object on the server. Originally I cracked this by saving the file to disk, reading it into the XmlDocument object and then deleting the file. The only problem was that the delete action was trying to take place before the XmlDocument.Load action had completed. It felt like an ugly solution anyway, so it was happily abandoned.

Next effort was to read directly from the Request.Files[x].InputStream directly into the XmlDocument, but I'm having problems. The following code fails with a 'Root element is missing' error, but I know the XML is valid so it must be something else.

        foreach (string file in Request.Files) 
        {
            HttpPostedFileBase postedFile = Request.Files[file] as  HttpPostedFileBase;

            if (postedFile.ContentLength > 0) //if file not empty
            {
               //create an XML object and load it in
                XmlDocument xmlProjPlan = new XmlDocument();

                Stream fileStream = postedFile.InputStream;
                byte[] byXML = new byte[postedFile.ContentLength];
                fileStream.Read(byXML, 0, postedFile.ContentLength);
                xmlProjPlan.Load(fileStream);
             }
       }

Any suggestions would be excellent, thank you.

Jonathan


You can read about progress at http://ablesoftware.blogspot.com/

+1  A: 

So a few things look wrong.

fileStream.Read(byXML, 0, postedFile.ContentLength); 

This line reads the file into the byXML byte buffer, but you are not utilizing this byte buffer later on, so I think you meant to remove this line or use the byXML buffer for your XmlDocument.Load() instead of the fileStream.

This line is unfortunately advancing your stream to the end, so when you call

xmlProjPlan.Load(fileStream); 

It's getting nothing because the stream is already at the end. That's probably why it can't find a root element.

Jimmy Hoffa
Brilliant. Thanks for that. It makes sense and I did wonder about that.It's what you get from blindly copy-and-pasting from example code that you're not sure about.
JonRed
A: 

Here's an example:

<% using (Html.BeginForm("index", "home", FormMethod.Post, new { enctype = "mutipart/form-data" })) { %>
    <input type="file" name="file" />
    <input type="submit" value="Upload" />
<% } %>

And the controller action:

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0 && file.ContentType == "text/xml")
    {
        var document = new XmlDocument();
        document.Load(file.InputStream);
        // TODO: work with the document here
    }
    return View();
}
Darin Dimitrov
Thanks for the note on ContentType validation - saves me the try and catch code I had put in place for this.
JonRed