【问题标题】:Deserialize XML to List<string> failing with XmlArray, XmlElement, and XmlArrayItem使用 XmlArray、XmlElement 和 XmlArrayItem 将 XML 反序列化为 List<string>
【发布时间】:2015-03-23 13:58:19
【问题描述】:

我有一个很大的对象,并且包含其他几个对象。整个东西是从一个 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; }

}

问题在于“ContentHierarchy”。 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&lt;string&gt; 的计数为 0。

我做错了什么?

【问题讨论】:

    标签: c# xml serialization deserialization


    【解决方案1】:

    尝试将属性 [System.Xml.Serialization.XmlArrayItemAttribute("Content", IsNullable = false)] 放在 ContentHierarchy 属性之上。在类构造函数中,属性名称为 ContentHierarchy,请检查您的属性是否也具有名称 ContentHierarchy(错误)或 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)));
        }
    
    }
    

    【讨论】:

    • 我想避免创建一个(另一个)类 - 这是从这个 XML 中的 Root 对象向下的两个或三个对象,并且访问它的行已经很长了。如何更改 XML 以使现有代码正常工作?
    • 尝试将属性 [System.Xml.Serialization.XmlArrayItemAttribute("Content", IsNullable = false)] 放在 ContentHierarchy 属性之上
    • 我现在就试试,但我不抱太大希望,因为“内容”不是属性,除非我的术语有误。编辑:不,那没有用。还是 0 个结果。
    • 奇怪,我正在测试并且列表已填满。我将在上面粘贴我的完整代码
    • 所以我做了一些进一步的测试,根据你给我的建议,我在列表中添加了一些条目(以编程方式)并序列化对象......它给了我与失败相同的 XML反序列化。
    猜你喜欢
    • 2015-05-30
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多