c# - 使用未包装的集合反序列化 XML 时遇到问题

标签 c# xml-deserialization

使用 C# 反序列化以下 XML 的正确方法是什么?

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gCal='http://schemas.google.com/gCal/2005'>
    <id>http://www.google.com/cal...</id>
    <subtitle type='text'>RockPointe Events (1)</subtitle>
    <entry>
        <id>http://www.google.com/cal...</id>
        <published>2011-07-07T21:43:44.000Z</published>
        <updated>2011-07-07T21:48:31.000Z</updated>
        <title type='html'>Event 1</title>
        <summary type='html'>Event 1 Summary</summary>
        <content type='html'>Event 1 Content</content>
    </entry>
    <entry>
        <id>http://www.google.com/cal...</id>
        <published>2011-07-07T21:43:44.000Z</published>
        <updated>2011-07-07T21:48:31.000Z</updated>
        <title type='html'>Event 2</title>
        <summary type='html'>Event 2 Summary</summary>
        <content type='html'>Event 2 Content</content>
    </entry>
</feed>

这是我当前的 POCO

    [XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom")]
    public class Feed
    {
        [XmlElement("subtitle")]
        public string Subtitle { get; set; }

        [XmlElement("title")]
        public string Title { get; set; }

        [XmlElement("entry")]
        public m_Entry[] Entry { get; set; }

        [XmlType(Namespace = "")]
        public class m_Entry
        {
            [XmlElement("title")]
            public string Title { get; set; }

            [XmlElement("summary")]
            public string Summary { get; set; }

            [XmlElement("content")]
            public string Content { get; set; }

            [XmlElement("published")]
            public DateTime Published { get; set; }

            [XmlElement("updated")]
            public DateTime Updated { get; set; }
        }
    }

当我通过 Deserialize 方法运行它时,我按预期获得了 TitleSubtitle 。问题在于entry。我得到了两个条目,但一切都是空的。

null entry

最佳答案

嗯,看起来就像从 Entry Class 中删除 XmlType 属性一样简单

[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom")]
public class Feed
{
    [XmlElement("subtitle")]
    public string Subtitle { get; set; }

    [XmlElement("title")]
    public string Title { get; set; }

    [XmlElement("entry")]
    public Entry[] Entries { get; set; }

    public class Entry
    {
        [XmlElement("title")]
        public string Title { get; set; }

        [XmlElement("summary")]
        public string Summary { get; set; }

        [XmlElement("content")]
        public string Content { get; set; }

        [XmlElement("published")]
        public DateTime Published { get; set; }

        [XmlElement("updated")]
        public DateTime Updated { get; set; }
    }
}

关于c# - 使用未包装的集合反序列化 XML 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9623962/

相关文章:

c# - 可以从编辑器模板中调用显示模板吗?

c# - 如何在网站中部署 win32 dll 以便它可以在 activex 对象中使用?

c# - 如何获得linq中最高价和最低价商品的数量之和

java - 使用 SimpleXML 反序列化

c# - XML Unicode 反序列化

c# - 根据 ID 从 List<T> 中获取一个元素

c# - 异步取自阻塞集合

c# - 正则表达式匹配 IP

c# - 序列化对象列表

xml - 使用 Jackson : no String-argument constructor/factory method to deserialize from String value 从 XML 到 POJO 的反序列化问题