0

I have an API function to upload xml files and it will return it as a object. It's made to read Italy's digital invoice (which is basically an xml file) but it says it's invalid.

InvalidOperationException: <FatturaElettronica xmlns='http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2'> was not expected.

The function is:

    [HttpPost]
    public ActionResult PostFile(IFormFile upload)
    {
        XmlSerializer ser = new XmlSerializer(typeof(FatturaElettronica));
        Stream memStream = upload.OpenReadStream();
        FatturaElettronica fe = (FatturaElettronica)ser.Deserialize(memStream);
        return Ok();
    }

Where FatturaElettronica is the xml model/class. Here is the xml sample I used.

And here is the class (the first part)

using System.Xml.Serialization;

namespace backEnd.Models
{
    [XmlRoot("FatturaElettronica")]
    public class FatturaElettronica
    {
        [XmlElement("FatturaElettronicaHeader")]
        public FatturaElettronicaHeader FatturaElettronicaHeader { get; set; }

        [XmlArray("FatturaElettronicaBody")]
        public FatturaElettronicaBody[] FatturaElettronicaBody { get; set; }
    }
}

Which contains sub-classes like FatturaElettronicaHeader (structured like the xml file)

using System.Xml.Serialization;

namespace backEnd.Models
{ 
    public class FatturaElettronicaHeader
    {
        [XmlElement("DatiTrasmissione")]
        public DatiTrasmissione DatiTrasmissione { get; set; }
        [XmlElement("CedentePrestatore")]
        public CedentePrestatore CedentePrestatore { get; set; }
        [XmlElement("RappresentanteFiscale")]
        public RappresentanteFiscale? RappresentanteFiscale { get; set; }
        [XmlElement("CessionarioCommittente")]
        public CessionarioCommittente CessionarioCommittente { get; set; }
        [XmlElement("TerzoIntermediarioOSoggettoEmittente")]
        public TerzoIntermediarioOSoggettoEmittente? TerzoIntermediarioOSoggettoEmittente { get; set; }
        [XmlElement("SoggettoEmittente")]
        public string? SoggettoEmittente { get; set; }
    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Danny220
  • 51
  • 3
  • Please post the XML in the question. Thanks. – Yong Shun Feb 14 '23 at 08:48
  • I already did, but [here](https://pastebin.com/Dnx1sFPs) it is – Danny220 Feb 14 '23 at 08:55
  • There is a namespace on the root element, the `[XmlRoot("FatturaElettronica")]` needs to take that into account with e.g. `[XmlRoot(Namespace="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2",ElementName="FatturaElettronica")]`. – Martin Honnen Feb 14 '23 at 09:07
  • Now it doesn't throw an exception but neighter the header (FatturaElettronicaHeader) or the body gets serialized, they just set to null – Danny220 Feb 14 '23 at 09:19
  • Then I would suggest using one of the tools from [Generate C# class from XML](https://stackoverflow.com/q/4203540/3744182) to generate your model, rather than trying to manually create c# classes corresponding to your XML. – dbc Feb 14 '23 at 14:39

0 Answers0