0

I have this xml : Respuesta.xml

<ns1:PayOnLine_3pResponse xmlns:ns1="https://implementacion.nps.com.ar/ws">
    <Respuesta p2:type="tns:RespuestaStruct_PayOnLine_3p" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
        <psp_ResponseCod p2:type="xsd:string">1</psp_ResponseCod>
        <psp_ResponseMsg p2:type="xsd:string">EN CURSO - En Comercio</psp_ResponseMsg>
        <psp_TransactionId p2:type="xsd:string">2502928</psp_TransactionId>
    </Respuesta>
</ns1:PayOnLine_3pResponse>

And this Class: PayOnLine_3pResponse.cs

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = @"https://implementacion.nps.com.ar/ws")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = @"https://implementacion.nps.com.ar/ws", IsNullable = false)]
[System.ServiceModel.XmlSerializerFormatAttribute(Style = System.ServiceModel.OperationFormatStyle.Rpc, SupportFaults = true)]
public partial class PayOnLine_3pResponse
{
    [System.Xml.Serialization.XmlElementAttribute(Namespace = @"https://implementacion.nps.com.ar/ws")]
    public Respuesta Respuesta { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "https://implementacion.nps.com.ar/ws")]
public partial class Respuesta
{
    public string psp_ResponseCod { get; set; }
    public string psp_ResponseMsg { get; set; }
    public string psp_TransactionId { get; set; }
    [System.Xml.Serialization.XmlAttributeAttribute]
    public string type { get; set; }
}

But, i can't deserialize it. The var obj are returning null

For example: Main.cs

var xml = File.ReadAllText("Respuesta.xml");
var obj = ToObject<PayOnLine_3pResponse>(xml);

    private static T ToObject<T>(string xml)
    {
        if (string.IsNullOrEmpty(xml))
        {
            return default(T);
        }

        XmlSerializer serializer = new XmlSerializer(typeof(T));
        XmlReaderSettings settings = new XmlReaderSettings();

        using (StringReader textReader = new StringReader(xml))
        {
            using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
            {
                return (T)serializer.Deserialize(xmlReader);
            }
        }
    }

Anybody can help me? I have tried generate the class with svcutil, xsd... But with the same result. I can't deserialize data from xml to obj.

bellow you can see the dependencies and the target framework

"dependencies": {
        "Microsoft.Extensions.Logging": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0-*",
        "Microsoft.Extensions.Logging.Abstractions": "1.0.0",
        "System.ServiceModel": "1.0.0",
        "System.ServiceModel.Duplex": "4.0.1",
        "System.ServiceModel.Http": "4.1.0",
        "System.ServiceModel.NetTcp": "4.1.0",
        "System.ServiceModel.Primitives": "4.1.0",
        "System.ServiceModel.Security": "4.0.1",
        "System.Private.ServiceModel": "4.1.0",
        "System.Text.Encodings.Web": "4.0.0",
        "System.Xml.XmlSerializer": "4.0.11",
        "System.Xml.XDocument": "4.0.11",
        "Microsoft.NETCore.App": {
            "type": "platform",
            "version": "1.0.0"
        }
    },
    "frameworks": {
       "netcoreapp1.0": {
         "imports": "dnxcore50"
       }
    }
Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
Marcelo Oliveto
  • 827
  • 1
  • 9
  • 21
  • You'll need to provide some more information. Are you targeting .NET Core or the Full .NET Framework? If .NET Core, did you use the https://visualstudiogallery.msdn.microsoft.com/c3b3666e-a928-4136-9346-22e30c949c08 to generate the models/proxy classes? – Tseng Sep 14 '16 at 15:55
  • I has tried with WCF Connected Services, but does not working with this endpoint: https://implementacion.nps.com.ar/ws.php?wsdl SOAP RPC – Marcelo Oliveto Sep 14 '16 at 16:09
  • As far as i know the svcutil won't create files that are compatible with .NET Core (unless they updated it recently). That's why the WCF Connected Services. What error are you receiving when you use the WCF Connected Services ? – Tseng Sep 14 '16 at 16:28
  • Scaffolding Code ... Attempting to download metadata from 'https://implementacion.nps.com.ar/ws.php?wsdl' using WS-Metadata Exchange and HttpGet. Generating files... C:\Users\moliv\AppData\Local\Temp\WCFConnectedService\2016_Sept._14_16_06_35\Reference.cs Updating project.json file ... Error:Unable to check out the current file. The file may be read-only or locked, or you may need to check the file out manually. – Marcelo Oliveto Sep 14 '16 at 19:08
  • 1
    Did you manually edit that XML in some way? The attribute `p2:type="tns:RespuestaStruct_PayOnLine_3p"` indicates that there should be a namespace with the prefix `"tns"` somewhere in the XML. For an explanation of the meaning of that attribute, see [xsi:type attribute messing up C# XML deserialization](https://stackoverflow.com/questions/36361653/xsitype-attribute-messing-up-c-sharp-xml-deserialization/36365689#36365689) – dbc Sep 14 '16 at 20:40
  • 1
    If the missing namespace corresponding to the `"tns:"` prefix were added to the XML, it would be possible to get this to work with `XmlSerializer`. See this fiddle: https://dotnetfiddle.net/28nP3o. – dbc Sep 14 '16 at 20:47

0 Answers0