Search code examples
c#asp.netxmlcontrollerrestsharp

Failing to correctly read file contents of IFormFile


I've got a web form which is simply a file input where the user can submit xml files which are being sent to my API.

My problem is coming when trying to read these files into an XmlDocument. I get the contents through as a string correctly, but when loading this into my xmlFile, I'm getting an error that says the syntax is incorrect (Though it all looks the same as in my original file).

I've tried a number of different ways to get the contents out of my IFormFile but each one seems to give me the same issue.

Controller:

public ActionResult ReadFiles()
    {            
        IFormFileCollection selectedFiles = Request.Form.Files;

        MemoryStream ms = new MemoryStream();
        List<IFormFile> fileList = selectedFiles.ToList();
        string[] fileNames = fileList.Select(x => x.FileName).ToArray();
        int i = 0;

        RestRequest request = new RestRequest(Method.POST);
        RestClient client = new RestClient("-EndPoint-");
        request.AlwaysMultipartFormData = true;
        request.AddHeader("Content-Type", "multipart/form-data");

        foreach (IFormFile file in fileList)
        {
            file.CopyTo(ms);
            request.AddFile("components", ms.ToArray(), fileNames[i]);
            i++;
        }


        IRestResponse response = client.Execute(request);   
    }

In my API I've tried various things:

Attempt #1

[HttpPost]
            public string Checks(IFormFileCollection components)
            {
    
                XmlDocument xmlFile = new XmlDocument();
                string[] arrFileNames = components.Select(x => x.FileName).ToArray();
                int fileNameCounter = 0;
                MemoryStream ms = new MemoryStream();
                List<IFormFile> fileList = components.ToList();
               
                foreach (IFormFile file in fileList)
                {
                    using (MemoryStream fileStream = new MemoryStream())
                    {
                        file.CopyTo(fileStream);
                        byte[] bytes = fileStream.ToArray();
                        string xmlString = Encoding.UTF8.GetString(bytes);
                        xmlFile.Load(xmlString);
                        //Do Work
                    }

Attempt #2

                StringBuilder xaml = new StringBuilder();
                using (var reader = new StreamReader(file.OpenReadStream()))
                {
                    while (reader.Peek() >= 0)
                        xaml.AppendLine(reader.ReadLine());
                }
                string xamlString = xaml.ToString();
                xmlFile.Load(xamlString);
                
                //Do Work
            }

Attempt 3

XmlDocument xml = new XmlDocument();
            using (MemoryStream ms = new MemoryStream())
            {
                fileList[0].CopyTo(ms);
                xml.Load(Encoding.UTF8.GetString(ms.ToArray()));

            }

Everything else I try seems to lead me to the same issue. When I Debug the code, the string is correct with my full xml file:

Debug String

But when I try to load to my Xml, I get this error: Debug Xml

It looks like it's taking part of the original file path in with the string or something for some reason, but even when I try manually substring'ing that out it still gives the same error.

Am I missing something obvious here?

Many thanks


Solution

  • I found one solution was to use XDocument instead of XmlDocument, which now all works fine!