4

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'

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);
     }
}

2 Answers 2

9

Here's an example:

<% using (Html.BeginForm("index", "home", FormMethod.Post, new { enctype = "multipart/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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the note on ContentType validation - saves me the try and catch code I had put in place for this.
Hey dude; Thanks for the answer it helped me....But I copied and pasted your code and was spent few hours why is not working. I finally found that you wrote: "mutipart" instead of "multipart". If you edit this post would be nice for others.
@freewill, good spot. I have updated my answer. Thanks for pointing this out.
4

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.

1 Comment

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.

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.