简体   繁体   中英

Deserialize XML to List<string> failing with XmlArray, XmlElement, and XmlArrayItem

I've got an object that is large, and contains several other objects. The entire thing is deserialized from an XML file.

Everything in the deserialization is working correctly except for the following:

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

}

The problem resides with 'ContentHierarchy'. The relevant section of the XML is as follows:

<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>

All of the non-list parts of this object deserialize correctly.

I'm trying to figure out what I need to use above the property declaration of ContentHierarchy to get the deserializer to populate it with the ContentHierarchy\\Content items from the XML file.

I have tried using XmlArray() , XmlArray(ContentHierarchy) , XmlElement(Content) , XmlElement(ContentHierarchy) , XmlArrayItem(Content) , and various combinations of the above.

Each time, the List<string> has a Count of 0.

What am I doing wrong?

Try to put the attribute [System.Xml.Serialization.XmlArrayItemAttribute("Content", IsNullable = false)] above the ContentHierarchy property. In the class constructor the property name is ContentHeirarchy, check if you property has the name ContentHeirarchy (wrong) or ContentHierarchy (right) too.

My full code:

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

}

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