将XML字符串反序列化为复杂类型

时间:2012-07-01 15:02:56

标签: c# xml deserialization

我的Xml(我无法改变):

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

我的模特:

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

反序列化此XML按预期工作但我想将Country属性更改为复杂类型,如下所示:

public Country Country { get; set; }

并将国家名称“JAPAN”放在Country.Name属性中,是否有任何想法?

2 个答案:

答案 0 :(得分:7)

您可以使用[XmlText]属性装饰Name类的Country属性,如下所示:

[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; }
}

还要注意我已经摆脱了[Serializable]属性。它对XML序列化没用。此属性用于二进制/远程处理序列化。

这是一个完整的示例,将按预期打印JAPAN

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);
        }
    }
}

答案 1 :(得分:0)

这是VB 2010的等价物......

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