简体   繁体   中英

C# how to deserialize a single nested class from xml

My C# class:

[XmlRoot("ResultDetails")]
public class MyClass
{
    [XmlElement("Id")] 
    public string Id { get; set; }
    [XmlElement("Name")] 
    public string Name { get; set; }
}

I'm receiving the XML from a SOAP request, so the soap body looks like this:

 <soap:Body>
        <Response xmlns="http://services.serviceprovider.co.uk/">
            <Result>
                <ResultDetails>
                    <Id>3636346</Id>
                    <Name>MyName</Name>
                </ResultDetails>
            </Result>
        </Response>
    </soap:Body>

And trying to deserialize it like this:

var xmlResponse = XDocument.Load(soapResult);
var myClassRootElement = (from xElement in xmlResponse.Descendants()
    where xElement.Name.LocalName == "ResultDetails"
    select xElement).First();
var serializer = new XmlSerializer(typeof(MyClass));
var myClass= (MyClass)serializer.Deserialize(myClassRootElement.CreateReader());

The last line is throwing the following exception:

System.InvalidOperationException: There is an error in XML document (0, 0). ---> System.InvalidOperationException: <ResultDetails xmlns="http://services.serviceprovider.co.uk/"> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly

Which I don't understand why, because surely as I've set ResultDetails as the XmlRoot of MyClass it should be exactly what the deserializer was expecting?

Try following

using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApp2
{
    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://www.w3.org/2003/05/soap-envelope/")]
    public class Envelope
    {
        public Body Body { get; set; }
    }
    public class Body
    {
        public Response Response { get; set; }
    }
    public class Response
    {
        [XmlArray(ElementName = "Result")]
        [XmlArrayItem("ResultDetails")]
        public List<MyClass> MyClass { get; set; }
    }
    [XmlRoot("ResultDetails")]
    public class MyClass
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }



}

Used Xml

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/">
    <soap:Body>
        <Response xmlns="http://services.serviceprovider.co.uk/">
            <Result>
                <ResultDetails>
                    <Id>3636346</Id>
                    <Name>MyName</Name>
                </ResultDetails>
            </Result>
        </Response>
    </soap:Body>
</soap:Envelope>

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