使用XmlArray,XmlElement和XmlArrayItem将XML反序列化为List <string>失败</string>

时间:2015-03-23 13:58:20

标签: c# xml serialization deserialization

我有一个很大的对象,并且包含其他几个对象。整个过程都是从XML文件中反序列化的。

反序列化中的所有内容都正常工作,但以下情况除外:

[XmlTypeAttribute("ClientInformation")]
public class ClientInfo
{
    public ClientInfo()
    {
        ClientName = "testclient";
        ClientDisplayFullName = "testclient";
        ClientDisplayShortName = "testclient";
        ContentClientName = "";
        ContentHeirarchy = new List<string>();
    }

    /// <summary>
    /// The "ShortName" for this client (URL Friendly)
    /// </summary>
    public string ClientName { get; set; }

    /// <summary>
    /// This is used to determine what content path to use for content.
    /// normally the same as the ClientName.
    /// </summary>
    public string ContentClientName { get; set; }

    public List<string> ContentHierarchy { get; set; }

    public string ClientDisplayFullName { get; set; }

    public string ClientDisplayShortName { get; set; }

}

问题在于&#39; ContentHierarchy&#39;。 XML的相关部分如下:

<ClientInformation>
    <ClientName>ABC</ClientName>
    <ContentClientName>ABC</ContentClientName>
    <ClientDisplayFullName>ABC&amp;D</ClientDisplayFullName>
    <ClientDisplayShortName>ABC&amp;D</ClientDisplayShortName>
    <ContentHierarchy>
        <Content>XYZB</Content>
        <Content>Base</Content>
    </ContentHierarchy>
</ClientInformation>

此对象的所有非列表部分都正确反序列化。

我试图弄清楚我需要在ContentHierarchy的属性声明之上使用什么来让反序列化器用XML文件中的ContentHierarchy \ Content项填充它。

我尝试过使用XmlArray()XmlArray(ContentHierarchy)XmlElement(Content)XmlElement(ContentHierarchy)XmlArrayItem(Content)以及上述的各种组合。

每次List<string>的计数为0。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试将属性[System.Xml.Serialization.XmlArrayItemAttribute(&#34; Content&#34;,IsNullable = false)]置于ContentHierarchy属性上方。在类构造函数中,属性名称为ContentHeirarchy,检查您的属性是否具有名称ContentHeirarchy(错误)或ContentHierarchy(右)。

我的完整代码:

    [XmlTypeAttribute("ClientInformation")]
public class ClientInfo
{
    public ClientInfo()
    {
        ClientName = "testclient";
        ClientDisplayFullName = "testclient";
        ClientDisplayShortName = "testclient";
        ContentClientName = "";
        ContentHierarchy = new List<string>();
    }

    /// <summary>
    /// The "ShortName" for this client (URL Friendly)
    /// </summary>
    public string ClientName { get; set; }

    /// <summary>
    /// This is used to determine what content path to use for content.
    /// normally the same as the ClientName.
    /// </summary>
    public string ContentClientName { get; set; }

    [System.Xml.Serialization.XmlArrayItemAttribute("Content", IsNullable = false)]
    public List<string> ContentHierarchy { get; set; }

    public string ClientDisplayFullName { get; set; }

    public string ClientDisplayShortName { get; set; }

}

class Program
{
    static void Main(string[] args)
    {
        string xml = "<ClientInformation>    <ClientName>ABC</ClientName>    <ContentClientName>ABC</ContentClientName>    <ClientDisplayFullName>ABC&amp;D</ClientDisplayFullName>    <ClientDisplayShortName>ABC&amp;D</ClientDisplayShortName>    <ContentHierarchy>        <Content>XYZB</Content>        <Content>Base</Content>    </ContentHierarchy></ClientInformation>";
        var xmlSerializer = new XmlSerializer(typeof(ClientInfo));
        var a = xmlSerializer.Deserialize(new MemoryStream(UTF8Encoding.Default.GetBytes(xml)));
    }

}