0

I used xmltocsharp site to create the class helper to deserialize an specific XML, but is not working, and the problem is in the root element. This is the root element (RESP_HDR and RESP_BODY were collapsed):

<?xml version="1.0" encoding="UTF-8"?>
<SII:RESPUESTA xmlns:SII="http://www.sii.cl/XMLSchema">
    + <SII:RESP_HDR>
    + <SII:RESP_BODY>
</SII:RESPUESTA>

And this is the root element class generated by xmltocsharp site:

[XmlRoot(ElementName = "RESPUESTA", Namespace = "http://www.sii.cl/XMLSchema")]
public class RESPUESTA
{
    [XmlElement(ElementName = "RESP_HDR", Namespace = "http://www.sii.cl/XMLSchema")]
    public RESP_HDR RESP_HDR { get; set; }
    [XmlElement(ElementName = "RESP_BODY", Namespace = "http://www.sii.cl/XMLSchema")]
    public RESP_BODY RESP_BODY { get; set; }
    [XmlAttribute(AttributeName = "SII", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string SII { get; set; }
}

The issue is that the class fails to deserialize a XML like the showed before, but success with this:

<?xml version="1.0" encoding="UTF-8"?>
<RESPUESTA xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SII="http://www.sii.cl/XMLSchema" xmlns="http://www.sii.cl/XMLSchema">
    + <SII:RESP_HDR>
    + <SII:RESP_BODY>
</RESPUESTA>

The difference is in the namespaces, even if a create the object and serialize it, this will be the result. So, what should be changed in the class to make it work with the original XML?

UPDATE:


Looking closer I found the really issue, is in the root element still, but I notice the missing xmlns prefix in the root tag, how can I set it in the helper class?

EDIT:


This is an XML sample from the service response:

<?xml version="1.0" encoding="UTF-8"?>
<SII:RESPUESTA xmlns:SII="http://www.sii.cl/XMLSchema">
    <SII:RESP_HDR>
        <SII:ESTADO>0</SII:ESTADO>
        <SII:GLOSA/>
    </SII:RESP_HDR>
    <SII:RESP_BODY>
        <DATOS_CONSULTA>
            <RUT>80182144-3</RUT>
            <TIPO_CONSULTA>DEUDOR</TIPO_CONSULTA>
            <DESDE_DDMMAAAA>01042017</DESDE_DDMMAAAA>
            <HASTA_DDMMAAAA>01052017</HASTA_DDMMAAAA>
        </DATOS_CONSULTA>
        <CESION>
            <VENDEDOR>11455447-9</VENDEDOR>
            <ESTADO_CESION>Cesion Vigente</ESTADO_CESION>
            <DEUDOR>80182144-3</DEUDOR>
            <MAIL_DEUDOR/>
            <TIPO_DOC>33</TIPO_DOC>
            <NOMBRE_DOC>Factura Electronica</NOMBRE_DOC>
            <FOLIO_DOC>107</FOLIO_DOC>
            <FCH_EMIS_DTE>2017-04-04</FCH_EMIS_DTE>
            <MNT_TOTAL>3324860</MNT_TOTAL>
            <CEDENTE>11455447-9</CEDENTE>
            <RZ_CEDENTE>JHON DOE</RZ_CEDENTE>
            <MAIL_CEDENTE>jjdoe@gmail.com</MAIL_CEDENTE>
            <CESIONARIO>762327129-7</CESIONARIO>
            <RZ_CESIONARIO>capital sa</RZ_CESIONARIO>
            <MAIL_CESIONARIO>xcap@capital.com</MAIL_CESIONARIO>
            <FCH_CESION>2017-04-05 13:15</FCH_CESION>
            <MNT_CESION>3324860</MNT_CESION>
            <FCH_VENCIMIENTO>2017-06-04</FCH_VENCIMIENTO>
        </CESION>
        <CESION>
            <VENDEDOR>11455447-9</VENDEDOR>
            <ESTADO_CESION>Cesion Vigente</ESTADO_CESION>
            <DEUDOR>80182144-3</DEUDOR>
            <MAIL_DEUDOR/>
            <TIPO_DOC>33</TIPO_DOC>
            <NOMBRE_DOC>Factura Electronica</NOMBRE_DOC>
            <FOLIO_DOC>34</FOLIO_DOC>
            <FCH_EMIS_DTE>2017-03-01</FCH_EMIS_DTE>
            <MNT_TOTAL>1725500</MNT_TOTAL>
            <CEDENTE>11455447-9</CEDENTE>
            <RZ_CEDENTE>JOE DOE</RZ_CEDENTE>
            <MAIL_CEDENTE>jd@gmail.com</MAIL_CEDENTE>
            <CESIONARIO>762327129-7</CESIONARIO>
            <RZ_CESIONARIO>Capital S.A.</RZ_CESIONARIO>
            <MAIL_CESIONARIO>jcap@capital.com</MAIL_CESIONARIO>
            <FCH_CESION>2017-04-05 17:27</FCH_CESION>
            <MNT_CESION>1725500</MNT_CESION>
            <FCH_VENCIMIENTO>2017-03-01</FCH_VENCIMIENTO>
        </CESION>
    </SII:RESP_BODY>
</SII:RESPUESTA>
Yasel
  • 2,920
  • 4
  • 40
  • 48
  • Both XML are identical. They have the same element names and the same namespaces. The namespace prefixes are irrelevant. – Alexander Petrov May 25 '17 at 22:38
  • Still not working, the only way so far that I can make it work is modifying the obtained XML, add the prefix SII to the tag `RESPUESTA` and add the rest of the namespaces, then deserialize this XML. – Yasel May 26 '17 at 13:53
  • I meant "remove" the prefix SII from the tag `RESPUESTA` – Yasel May 26 '17 at 15:05

2 Answers2

0

So far the only way that I can make it work is with a really ugly solution, this should not be considered as an answer to the problem!!.

//Query service to obtain XML response
string xmlResponse = siiClient.QueryDocuments(documentsRequest);

//Replace RESPUESTA tags in the XML response, remove the prefix SII
var replacedXML = xmlResponse .Replace("SII:RESPUESTA", "RESPUESTA" );

//Load XML string into XmlDocument
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(replacedXML);

//Add missing namespaces
xDoc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
xDoc.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
xDoc.DocumentElement.SetAttribute("xmlns", "http://www.sii.cl/XMLSchema");

//Now deserialization will work
var documentResponse = xDoc.ParseXML<RESPUESTA>();

The ideal solution will take the XML Response and deserialize it directly without any preprocessing like:

//Query service to obtain XML response
string xmlResponse = siiClient.QueryDocuments(documentsRequest);

//ParseXML is an extension method, it can handle an string or an XmlDocument
var documentResponse = xmlResponse.ParseXML<RESPUESTA>();

Note: ParseXML is based on @Damian's answer

Yasel
  • 2,920
  • 4
  • 40
  • 48
  • I just added more details to the question and also a comment to your answer, thank you for your attention. – Yasel May 26 '17 at 20:23
  • @AlexanderPetrov You're correct it is valid XML either way, however you often encounter custom written XML validation which will insist the XML elements are structured a certain way, it's what led me here, some idiot insisting I add the prefix to the root element :| This answer is terrible btw, just don't do this. – Tony Cheetham Mar 29 '18 at 12:14
  • 1
    @tonyenkiducx - I agree. Customers often insist on a particular format. I'm trying to explain to them, waving the xml standard... not helps. – Alexander Petrov Mar 29 '18 at 12:43
  • I figured out what is most likely the issues anyway, related to not posting all your code ;) Your serializer might be missing the hints for the namespace so no prefix is added, or you might be adding a blank root attribute to your serializer(https://stackoverflow.com/questions/49556390/c-sharp-serializer-not-adding-prefix-to-root-element) – Tony Cheetham Mar 29 '18 at 12:49
0

I took your xml:

<?xml version="1.0" encoding="UTF-8"?>
<SII:RESPUESTA xmlns:SII="http://www.sii.cl/XMLSchema">
  <SII:RESP_HDR/>
  <SII:RESP_BODY/>
</SII:RESPUESTA>

I took you class:

[XmlRoot(ElementName = "RESPUESTA", Namespace = "http://www.sii.cl/XMLSchema")]
public class RESPUESTA
{
    [XmlElement(ElementName = "RESP_HDR", Namespace = "http://www.sii.cl/XMLSchema")]
    public RESP_HDR RESP_HDR { get; set; }
    [XmlElement(ElementName = "RESP_BODY", Namespace = "http://www.sii.cl/XMLSchema")]
    public RESP_BODY RESP_BODY { get; set; }
    [XmlAttribute(AttributeName = "SII", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string SII { get; set; }
}
public class RESP_HDR { }
public class RESP_BODY { }

Just added two empty class stub.

Try System.Xml.Serialization.XmlSerializer:

RESPUESTA respuesta;

var xs = new XmlSerializer(typeof(RESPUESTA));

using (var fs = new FileStream("test.xml", FileMode.Open))
    respuesta = (RESPUESTA)xs.Deserialize(fs);

It works! I don't understand how and why it does not work for you.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • Petrov, I never mentioned having an error trying to deserialize, is just that the result is an empty object, just like the one you get. I have added an XML sample, from the service response, maybe you can try once more. Remember that I generate the class in http://xmltocsharp.azurewebsites.net/ – Yasel May 26 '17 at 20:22