2

我的 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; }

并在Country.Name属性中输入国家名称“JAPAN”,有什么想法吗?

4

2 回答 2

7

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

[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);
        }
    }
}
于 2012-07-01T15:09:45.303 回答
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
于 2012-07-25T14:55:32.113 回答