Deserialize XML string to complex type

北城余情 提交于 2019-12-11 02:23:20

问题


My Xml (which I can't change):

<result>
    <type>MAZDA</type>
    <make>RX-8</type>
    <country>JAPAN</country>
</result>

My model:

[Serializable, XmlRoot("result")]
public class VehicleDetails
{
    public string Type { get; set; }
    public string Make { get; set; }
    public string Country { get; set; }
}

de-serializing this XML works as expected but I want to change the Country property to a complex type, like so:

public Country Country { get; set; }

and put the country name, "JAPAN", in the Country.Name property, any ideas?


回答1:


You could decorate the Name property of your Country class with the [XmlText] attribute like this:

[XmlRoot("result")]
public class VehicleDetails
{
    public string Type { get; set; }
    public string Make { get; set; }
    public Country Country { get; set; }
}

public class Country
{
    [XmlText]
    public string Name { get; set; }
}

Also notice that I have gotten rid of the [Serializable] attribute. It is useless for XML serialization. This attribute is used for binary/remoting serialization.

And here's a full example that will print JAPAN as expected:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("result")]
public class VehicleDetails
{
    public string Type { get; set; }
    public string Make { get; set; }
    public Country Country { get; set; }
}

public class Country
{
    [XmlText]
    public string Name { get; set; }
}

class Program
{
    static void Main()
    {
        var serializer = new XmlSerializer(typeof(VehicleDetails));
        var xml = 
        @"<result>
            <Type>MAZDA</Type>
            <Make>RX-8</Make>
            <Country>JAPAN</Country>
        </result>";
        using (var reader = new StringReader(xml))
        using (var xmlReader = XmlReader.Create(reader))
        {
            var result = (VehicleDetails)serializer.Deserialize(xmlReader);
            Console.WriteLine(result.Country.Name);
        }
    }
}



回答2:


Here is the VB 2010 equivalent...

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Public Module Module1

    Public Sub Main()
        Dim serializer = New XmlSerializer(GetType(VehicleDetails))
        Dim xml = "<result>             <Type>MAZDA</Type>             <Make>RX-8</Make>             <Country>JAPAN</Country>         </result>"

        Using reader = New StringReader(xml)
            Using xmlReader__1 = XmlReader.Create(reader)
                Dim result = DirectCast(serializer.Deserialize(xmlReader__1), VehicleDetails)
                Console.WriteLine(result.Country.Name)
            End Using
        End Using
    End Sub

    <XmlRoot("result")> _
    Public Class VehicleDetails

        Public Property Type() As String
            Get
                Return m_Type
            End Get
            Set(value As String)
                m_Type = value
            End Set
        End Property
        Private m_Type As String


        Public Property Make() As String
            Get
                Return m_Make
            End Get
            Set(value As String)
                m_Make = value
            End Set
        End Property
        Private m_Make As String
        Public Property Country() As Country
            Get
                Return m_Country
            End Get
            Set(value As Country)
                m_Country = value
            End Set
        End Property
        Private m_Country As Country

    End Class
    Public Class Country
        <XmlText()> _
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(value As String)
                m_Name = value
            End Set
        End Property
        Private m_Name As String
    End Class

End Module


来源:https://stackoverflow.com/questions/11283040/deserialize-xml-string-to-complex-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!