I have an XML that i am trying to serialize to a class but i am unable to fathom why the object is null. null object
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<ns0:vendResponse xmlns:ns0="http://mynamespace.co/ns0">
<ns0:sequence>234532532221</ns0:sequence>
<ns0:CId>0</ns0:CId>
<ns0:RefId>202675454545453434343</ns0:RefId>
<ns0:origAcct>20.00</ns0:origAcct>
<ns0:destAcct>00087646564</ns0:destAcct>
<ns0:responseCode>0</ns0:responseCode>
<ns0:responseMessage>Successful</ns0:responseMessage>
</ns0:vendResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
var contentStream = File.ReadAllText("response.xml");
Envelope resultEnvelope = new Envelope();
using (var stringReader = new StringReader(contentStream))
{
using (XmlReader reader = new XmlTextReader(stringReader))
{
var serializer1 = new XmlSerializer(typeof(Envelope));
resultEnvelope = serializer1.Deserialize(reader) as Envelope;
}
}
the soap Envelope class generated in VS using paste XML as classes:
CodePudding user response:
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
namespace ConsoleApplication23
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
Envelope envelope = (Envelope)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement("Body")]
public Body body { get; set; }
}
public class Body
{
[XmlElement(ElementName = "vendResponse", Namespace = "http://mynamespace.co/ns0")]
public VendResponse vendResponse { get; set; }
}
public class VendResponse
{
public string sequence { get; set; }
public string CId { get; set; }
public string RefId { get; set; }
public string origAcct { get; set; }
public string destAcct { get; set; }
public int responseCode { get; set; }
public string responseMessage { get; set; }
}
}