0

I need to serialize an xml schema in c#. The problem is that I don't know how to serialize the paragraph element of this schema.

    <?xml version="1.0" encoding="UTF-8"?>
<p:EsitoRichiestaCertificatoDispositivo xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/corrispettivi/v1.0" versione="1.0">
</p:EsitoRichiestaCertificatoDispositivo>

This is the class i wrote:

 [Serializable]
    [XmlRootAttribute(Namespace = "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/corrispettivi/v1.0", IsNullable = false)]
    public class EsitoRichiestaCertificatoDispositivo
    {
        public string IdOperazione;
        [SoapElement(DataType = "base64Binary")]
        public string Certificato;
        public ErroriType Errori;

        public EsitoRichiestaCertificatoDispositivo()
        {
            IdOperazione = "";
            Certificato = "";
            Errori = new ErroriType();
        }
    }

This is the xml schema: https://www.agenziaentrate.gov.it/portale/documents/20143/296358/Provvedimento+30+marzo+2017+Distributori+automatici_CorrispettiviMessaggiTypes_v1.0.xsd/69bec22c-92f1-7c6a-f5db-ad213b93443d

DaX
  • 1
  • 1
  • 3
  • Yes this should be the resulting xml of a serialization – DaX Nov 12 '22 at 12:48
  • To serialize you need to crate an instance of the class like this : EsitoRichiestaCertificatoDispositivo certificate = new EsitoRichiestaCertificatoDispositivo(); Then set properties in the class .The property is a string. So you need to put into the property a Base64 string using following : string Certificate = Convert.ToBase64String(byte[] array). If your certificate is already a base64 string than just assign the certificate to the property. – jdweng Nov 12 '22 at 15:16
  • My proble is the serialization and deserialization of the p tag element not the certificate...please help me – DaX Nov 12 '22 at 15:35
  • The following may be helpful: https://stackoverflow.com/a/74364149/10024425 and https://stackoverflow.com/a/68232320/10024425 – Tu deschizi eu inchid Nov 12 '22 at 15:37
  • 1
    P is not a tag, it is a namespace. – jdweng Nov 12 '22 at 15:40
  • That doesn't appear to be valid XML. Where's the rest of the XML? – Tu deschizi eu inchid Nov 12 '22 at 15:58
  • jdweng so how to declare this namespace for serialization in c#? – DaX Nov 12 '22 at 16:33
  • Try a search on web for `schema signature ivaservizi.agenziaentrate gov corrispettivi` My Spanish is not very good and trying to read the Argentina documents are not easy. – jdweng Nov 12 '22 at 21:02
  • I used the tool xsd.exe to convert xsd to c# classes. The tool gave error that signature type was missing in the schema. Usually this means that you need to add an include in the schema to reference another schema. Sometimes there are optional types in a schema and you need to modify the schema. You need to locate the schema that contains the signature type. Usually you can find at same location where you got schema. It may be that you are using an old version of schema that has an error. See https://www.w3schools.com/xml/el_include.asp – jdweng Nov 12 '22 at 21:03
  • The reason you are getting the namespace p is due to the URL in following xmlns:p=`"http://ivaservizi.agenziaentrate.gov.it/docs/xsd/corrispettivi/v1.0"` – jdweng Nov 12 '22 at 21:15

2 Answers2

0

Here is results I got using xsd.exe tool

enter image description here

jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Jdweng thank you for your help! Your suggestion to go through namespaces really helped me...after a lot of googling i've found the answer. Deserializing XML with namespace and multiple nested elements I really apreciate your help THANK U! THANK U! Thank U!!!!!!! This is my solution to the problem:

[Serializable]
[XmlRoot("EsitoRichiestaCertificatoDispositivo", Namespace = "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/corrispettivi/v1.0")]
public class EsitoRichiestaCertificatoDispositivo
{
    [XmlAttribute]
    public string versione = "1.0";

    [XmlElement(Namespace = "")]
    public string IdOperazione;

    [XmlElement(Namespace = "")]
    public string Certificato;
    [XmlElement(Namespace = "")]
    public ErroriType Errori;

    public EsitoRichiestaCertificatoDispositivo()
    {
        IdOperazione = "";
        Certificato = "";
        Errori = new ErroriType();
    }
}

and then when serializing:

public static XmlDocument SerializeToXml<T>(T source)
        {
            var document = new XmlDocument();
            var navigator = document.CreateNavigator();
            if (navigator != null)
            {
                using (var writer = navigator.AppendChild())
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("p", "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/corrispettivi/v1.0");
                    XmlSerializer xser = new XmlSerializer(typeof(T));
                    xser.Serialize(writer, source,ns);
                }
            }
            return document;
        }
DaX
  • 1
  • 1
  • 3