'Validating XML electronic invoice against XSD on C#
I'm creating an xml for the italian electronic invoice, this is the Model:
public class EInvoice
{
public class Fattura
{
[Serializable]
[XmlType(Namespace = "https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1.xsd")]
[XmlRoot("FatturaElettronica", Namespace = "https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1.xsd", IsNullable = false)]
public class FatturaElettronicaType
{
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public FatturaElettronicaHeaderType FatturaElettronicaHeader { get; set; }
[XmlElement("FatturaElettronicaBody", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public List<FatturaElettronicaBodyType> FatturaElettronicaBody { get; set; }
// <XmlElement([Namespace]:="http://www.w3.org/2000/09/xmldsig#")>
// Public Property Signature As SignatureType
[XmlAttribute]
public FormatoTrasmissioneType versione { get; set; }
}
[Serializable]
[XmlType(Namespace = "https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1.xsd")]
public class FatturaElettronicaHeaderType
{
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public DatiTrasmissioneType DatiTrasmissione { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public CedentePrestatoreType CedentePrestatore { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public RappresentanteFiscaleType RappresentanteFiscale { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public CessionarioCommittenteType CessionarioCommittente { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public TerzoIntermediarioSoggettoEmittenteType TerzoIntermediarioOSoggettoEmittente { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public SoggettoEmittenteType SoggettoEmittente { get; set; }
[XmlIgnore]
public bool SoggettoEmittenteSpecified { get; set; }
}
[Serializable]
[XmlType(Namespace = "https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1.xsd")]
public class DatiTrasmissioneType
{
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public IdFiscaleType IdTrasmittente { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "normalizedString")]
public string ProgressivoInvio { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public FormatoTrasmissioneType FormatoTrasmissione { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CodiceDestinatario { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ContattiTrasmittenteType ContattiTrasmittente { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string PECDestinatario { get; set; }
}
[Serializable]
[XmlType(Namespace = "https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1.xsd")]
public class IdFiscaleType
{
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string IdPaese { get; set; }
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string IdCodice { get; set; }
}
................................
}
}
And the xml generated is this:
<?xml version="1.0" encoding="utf-8"?>
<FatturaElettronica xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" versione="FPR12" xmlns="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2">
<FatturaElettronicaHeader xmlns="">
<DatiTrasmissione>
<IdTrasmittente>
<IdPaese>IT</IdPaese>
<IdCodice>06348743213</IdCodice>
</IdTrasmittente>
<ProgressivoInvio>1</ProgressivoInvio>
<FormatoTrasmissione>FPR12</FormatoTrasmissione>
<CodiceDestinatario>X2PH45J</CodiceDestinatario>
</DatiTrasmissione>
<CedentePrestatore>
<DatiAnagrafici>
<IdFiscaleIVA>
<IdPaese>IT</IdPaese>
<IdCodice>06348743213</IdCodice>
</IdFiscaleIVA>
<CodiceFiscale>06348743213</CodiceFiscale>
<Anagrafica>
<Denominazione>MyCompany</Denominazione>
</Anagrafica>
<RegimeFiscale>RF01</RegimeFiscale>
</DatiAnagrafici>
<Sede>
<Indirizzo>via Milano 26</Indirizzo>
<CAP>10126</CAP>
<Comune>Milano</Comune>
<Provincia>MI</Provincia>
<Nazione>IT</Nazione>
</Sede>
</CedentePrestatore>
<CessionarioCommittente>
<DatiAnagrafici>
<IdFiscaleIVA>
<IdPaese>IT</IdPaese>
<IdCodice>05841012217</IdCodice>
</IdFiscaleIVA>
<CodiceFiscale>05841012217</CodiceFiscale>
<Anagrafica>
<Denominazione>CompanyDenomination</Denominazione>
</Anagrafica>
</DatiAnagrafici>
<Sede>
<Indirizzo>Wall Street</Indirizzo>
<NumeroCivico>10</NumeroCivico>
<CAP>10095</CAP>
<Comune>New York</Comune>
<Provincia>NY</Provincia>
<Nazione>USA</Nazione>
</Sede>
</CessionarioCommittente>
</FatturaElettronicaHeader>
<FatturaElettronicaBody xmlns="">
<DatiGenerali>
<DatiGeneraliDocumento>
<TipoDocumento>TD01</TipoDocumento>
<Divisa>EUR</Divisa>
<Data>2020-11-13</Data>
<Numero>260/IT</Numero>
<ImportoTotaleDocumento>2000.00</ImportoTotaleDocumento>
</DatiGeneraliDocumento>
</DatiGenerali>
<DatiBeniServizi>
<DettaglioLinee>
<NumeroLinea>1</NumeroLinea>
<Descrizione>Consulenza</Descrizione>
<PrezzoUnitario>2000.00</PrezzoUnitario>
<PrezzoTotale>2000.00</PrezzoTotale>
<AliquotaIVA>22.00</AliquotaIVA>
<RiferimentoAmministrazione>4309882</RiferimentoAmministrazione>
</DettaglioLinee>
<DatiRiepilogo>
<AliquotaIVA>22.00</AliquotaIVA>
<ImponibileImporto>2000.00</ImponibileImporto>
<Imposta>440.00</Imposta>
</DatiRiepilogo>
</DatiBeniServizi>
<DatiPagamento>
<CondizioniPagamento>TP02</CondizioniPagamento>
<DettaglioPagamento>
<ModalitaPagamento>MP05</ModalitaPagamento>
<DataScadenzaPagamento>2021-01-31</DataScadenzaPagamento>
<ImportoPagamento>2000.00</ImportoPagamento>
</DettaglioPagamento>
</DatiPagamento>
</FatturaElettronicaBody>
</FatturaElettronica>
In the end I try to validate the xml in this way:
private static void Main(){
........
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create($"c:\\log\\{filname.xml}", settings);
// Parse the file.
while (reader.Read()) ;
}
// Display any warnings or errors.
private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message);
else
Console.WriteLine("\tValidation error: " + args.Message);
}
But vaildation failed with this message:
Cannot find schema information for the element 'https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1.xsd:FatturaElettronica'.
Why if the tag FatturaElettronica is the first element of my .xml?
Solution 1:[1]
Your XML has a default namespace. It should be taken into account.
Please try to add a couple of lines to your code:
c#
...
settings.Schemas.Add(null, new XmlTextReader(_schemaName)); // more efficient way to handle namespaces
settings.Schemas.Compile();
Solution 2:[2]
The Problem is that the xsd: https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2.1/Schema_del_file_xml_FatturaPA_versione_1.2.1.xsd is incorrect.
If you want to validate it online it shows severals errors.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yitzhak Khabinsky |
| Solution 2 | C1X |
