Xml Treatment with ASPNET MVC Core?

Asked

Viewed 397 times

2

I’m working on uploading Xml using the ASPNET MVC Core, I get in the controller the file this way:

public async Task<IActionResult> Upload(IFormFile param)
{
}

but I need to check it before I finish the code and upload it. My problem is reading the file through the XDocument, because the same does not accept this type of element that is coming.

Thus:

var dataXml = XDocument.Load(param)
                      .Descendants("ide")
                      .First()
                      .Element("dhEmi")
                      .Value;

Form for sending the file:

<form role="form" asp-action="Upload" enctype="multipart/form-data">
   <input type="file" name="id" id="id"  multiple />
   <input type="submit" value="Upload" />
</form>

I’ve tried to convert to string and other means but no success and someone has some idea?

  • Ask the question an XML sample to help understand the problem.

  • Good Afternoon Pagotti, as said the issue is not with xml but with reading by Xdocument that does not accept the way I receive the file in the controller, but anyway it is a simple NFE xml accurate to read the date of issue.

  • Okay. If you use param.OpenReadStream() in the Load inoperative?

  • I’ll try and tell you that option I haven’t tried yet

  • As is your form, can make available?

  • @Thiagomottabarboza you better add the c# tag in your question, it will make it easier to find people who can help you.

  • @Pagotti Openreadstream is recognized but it is not accepted to query in the xml used in the following code: var dataXml = Xdocument.Load(xml.Openreadstream()) . Descendants("ide") . First() . Element("dhEmi") . Value;

  • @Virgilionovic my form is simple, I have not used anything different follows added the code in the post

  • @Thiagomottabarboza I made the solution is that you are doing wrong in some points so I made two examples in the answer, if you can use any of the two reflecting on your ai... !!!

Show 4 more comments

1 answer

3

In the new development model ASP.NET Core, whether to use the Interface Iformfile who has the responsibility to work with the input of the kind file.

The problem that is happening is that the value should be a list and not 1 element, because its form configuration in the input is with multiple, example: List<IFormFile>.

Basic example:

Html

<form role="form" asp-action="Upload" enctype="multipart/form-data">
    <input type="file" name="id" id="id"  multiple />
    <input type="submit" value="Upload" />
</form>

Controller

public async Task<IActionResult> Upload(List<IFormFile> id)

being a factor also very important the name of input is the same name placed in the folder that will receive the files, example, you placed id in the name of input should follow the same name for the controller.

How you will receive the values being that your configuration was in a list?

I put together a minimal example to illustrate this, where this input can send multiple files to controller:

Xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <cliente>
    <id>1</id>
    <nome>stackoverflow</nome>
  </cliente>
</root>

Html

<form role="form" asp-action="Upload" enctype="multipart/form-data">
    <input type="file" name="id" id="id" multiple />
    <input type="submit" value="Upload" />
</form>

Controller

[AcceptVerbs("POST", "GET")]
public async Task<IActionResult> Upload(List<IFormFile> id)
{    
    if (Request.Method.Equals("POST"))
    {
        foreach (var formFile in id)
        {
            if (formFile.Length > 0)
            {
                using (MemoryStream str = new MemoryStream())
                {
                    formFile.CopyTo(str);
                    str.Position = 0;
                    var xml = (from x in XDocument.Load(str).
                        Descendants("cliente")
                              let _id = x.Element("id").Value
                              let _nm = x.Element("nome").Value
                              select new
                              {
                                  Id = _id,
                                  Nome = _nm
                              })
                              .FirstOrDefault();
                }
            }
        }
    }  
    return View();
}

In the example case I sent only one, but with the same layout of Xml if you can send several, if you prefer.


Remember that if it is only 1 file at a time just take the part of the collection as follows:

Xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <cliente>
    <id>1</id>
    <nome>stackoverflow</nome>
  </cliente>
</root>

Html

<form role="form" asp-action="Upload" enctype="multipart/form-data">
    <input type="file" name="id" id="id" />
    <input type="submit" value="Upload" />
</form>

Controller

[AcceptVerbs("POST", "GET")]
public async Task<IActionResult> Upload(IFormFile id)
{    
    if (Request.Method.Equals("POST"))
    {                
        if (id.Length > 0)
        {
            using (MemoryStream str = new MemoryStream())
            {
                await id.CopyToAsync(str);
                str.Position = 0;
                var xml = (from x in XDocument.Load(str).
                    Descendants("cliente")
                            let _id = x.Element("id").Value
                            let _nm = x.Element("nome").Value
                            select new
                            {
                                Id = _id,
                                Nome = _nm
                            })
                            .FirstOrDefault();
            }
        }               
    }  
    return View();
}

that is, in the input remove the setting multiple and in the controller leave only the interface Iformfile

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.