Home > Software design >  xml serialize paragraph C#
xml serialize paragraph C#

Time:11-13

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: enter image description here

CodePudding user response:

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;
        }
  • Related