简体   繁体   中英

There is an error in XML document (0, 0) - Error Deserializing XML to object

please advice how do i map the response into class because from every example i came across body always don't have symbol like core:transactionResponse

my code:

string fileName = @"C:\Users\Lenovo\Downloads\GLResponseXml.xml";
        XDocument xDoc = XDocument.Load(fileName);

        var unwrappedResponse = xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
                                .First()
                                .FirstNode;

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(TransactionResponse));
        TransactionResponse response = (TransactionResponse)xmlSerializer.Deserialize(xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
            .First()
            .FirstNode
            .CreateReader()
        );

for deserealizing this soap xml:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dpss0="bons">
    <soapenv:Header/>
    <soapenv:Body>
        <core:transactionResponse xmlns:bo="http://service.example.co.id/core/bo" xmlns:core="http://service.example.co.id/core">
            <response>
                <header>
                    <coreJournal>149326</coreJournal>
                </header>
                <content xsi:type="bo:OKMessage">
                    <message/>
                </content>
            </response>
        </core:transactionResponse>
    </soapenv:Body>
   </soapenv:Envelope>

but i got this error: InvalidOperationException: <transactionResponse xmlns='http://service.example.co.id/core'> was not expected.

i'm mapping the response into this class:

public class GLXmlResponse
{
    [XmlRoot(ElementName = "response")]
    public class Response
    {
        [XmlElement(ElementName = "header")]
        public Header Header { get; set; }
        [XmlElement(ElementName = "content")]
        public Content Content { get; set; }
    }

    [XmlRoot(ElementName = "header")]
    public class Header
    {
        [XmlElement(ElementName = "coreJournal")]
        public string CoreJournal { get; set; }
    }

    [XmlRoot(ElementName = "content")]
    public class Content
    {
        [XmlElement(ElementName = "message")]
        public string Message { get; set; }
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
    }

    [XmlRoot(ElementName = "transactionResponse", Namespace = "http://service.example.co.id/core")]
    public class TransactionResponse
    {
        [XmlElement(ElementName = "response")]
        public Response Response { get; set; }
        [XmlAttribute(AttributeName = "bo", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Bo { get; set; }
        [XmlAttribute(AttributeName = "core", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Core { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "transactionResponse", Namespace = "http://service.example.co.id/core")]
        public TransactionResponse TransactionResponse { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header { get; set; }
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soapenv { get; set; }
        [XmlAttribute(AttributeName = "soapenc", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soapenc { get; set; }
        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsd { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "dpss0", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Dpss0 { get; set; }
    }
}

need help on getting the class object right

I can't repro the exact error you're seeing, so you might want to check from a minimal repro; I'm using:

var env = (GLXmlResponse.Envelope)new XmlSerializer(typeof(GLXmlResponse.Envelope))
    .Deserialize(new StringReader(xml));        
System.Console.WriteLine(env.Body.TransactionResponse.Response.Header.CoreJournal);

where:

string xml = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:dpss0=""bons"">
<soapenv:Header/>
<soapenv:Body>
    <core:transactionResponse xmlns:bo=""http://service.example.co.id/core/bo"" xmlns:core=""http://service.example.co.id/core"">
        <response>
            <header>
                <coreJournal>149326</coreJournal>
            </header>
            <content xsi:type=""bo:OKMessage"">
                <message/>
            </content>
        </response>
    </core:transactionResponse>
</soapenv:Body></soapenv:Envelope>";

which just has a null for .Response . The reason for that is that this is actually in the empty namespace (prefixes are not inherited), so you need

[XmlElement(ElementName = "response", Namespace = "")]
public Response Response { get; set; }

However, this then causes a problem with the OKMessage in the xml. If I comment out the <content> , then it works and I can see the output 149326 .

However, it might be simpler to start by just copying the xml into the clipboard, Edit -> Paste Special -> Paste XML As Classes, and see what it generates, and work from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM