c# - 将 XML 反序列化为带有列表的对象

标签 c# xml xml-deserialization

我正在尝试将 xml 反序列化为一个对象,但它没有正确地通过 xml。它不会填充对象中的作者。我正在尝试返回一个对象,该对象由包含标题和作者列表的文章组成。作者列表不会填充此代码,这是我的问题。请帮忙,因为我是这个 XML 操作的新手。

在这里,您可以看到问题所在。 enter image description here

这是示例 xml。

<?xml version="1.0" encoding="UTF-8"?>
<MedlineCitationSet>
<Article>
    <ArticleTitle>Title 1</ArticleTitle>
    <AuthorList>
        <Author>
            <LastName>Public</LastName>
            <ForeName>J Q</ForeName>
            <Initials>JQ</Initials>
        </Author>
        <Author>
            <LastName>Doe</LastName>
            <ForeName>John</ForeName>
            <Initials>J</Initials>
        </Author>
    </AuthorList>
</Article>
<Article>
    <ArticleTitle>Title 2</ArticleTitle>
    <AuthorList>
        <Author>
            <LastName>Doe</LastName>
            <ForeName>John</ForeName>
            <Initials>J</Initials>
        </Author>
        <Author>
            <LastName>Doe</LastName>
            <ForeName>Jane</ForeName>
            <Initials>J</Initials>
        </Author>
    </AuthorList>
</Article>  
<Article>
    <ArticleTitle>Title 3</ArticleTitle>
    <AuthorList>
        <Author>
            <LastName>Doe</LastName>
            <ForeName>Jane</ForeName>
            <Initials>J</Initials>
        </Author>
        <Author>
            <LastName>Public</LastName>
            <ForeName>J Q</ForeName>
            <Initials>JQ</Initials>
        </Author>
    </AuthorList>
</Article>
<Article>
    <ArticleTitle>Title 4</ArticleTitle>
    <AuthorList>
        <Author>
            <LastName>Smith</LastName>
            <ForeName>John</ForeName>
            <Initials>J</Initials>
        </Author>
        <Author>
            <LastName>Doe</LastName>
            <ForeName>John</ForeName>
            <Initials>J</Initials>
        </Author>
    </AuthorList>
</Article>

这是我的类层次结构。

[XmlRoot("MedlineCitationSet")]
public class MedlineCitationSet
{
    [XmlElement("Article")]
    public List<Article> Articles { get; set; }
}

[XmlRoot("Article")]
public class Article
{
    [XmlElement("ArticleTitle")]
    public string ArticleTitle { get; set; }

    [XmlElement("AuthorList")]
    public List<Author> AuthorList { get; set; }
}

public class Author
{
    [XmlElement("LastName")]
    public string LastName { get; set; }

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

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

这是我的反序列化代码。

XmlSerializer serializer = new XmlSerializer(typeof(MedlineCitationSet));
using (FileStream fileStream = new FileStream(newPath + @"\XmlToRead\XmlToRead.xml", FileMode.Open))
{
    MedlineCitationSet result = (MedlineCitationSet)serializer.Deserialize(fileStream);
}

最佳答案

这部分:

[XmlElement("AuthorList")]
public List<Author> AuthorList { get; set; }

表示序列化程序处理每个 <AuthorList>元素作为作者,而不是额外的 <Author> xml 中的级别。

可以这样解决:

[XmlArray("AuthorList")]
[XmlArrayItem("Author")]
public List<Author> AuthorList { get; set; }

附言。您可以通过生成 MedlineCitationSet 轻松查看序列化程序对当前序列化映射的影响。在代码中并将其序列化。

关于c# - 将 XML 反序列化为带有列表的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40351650/

相关文章:

c# - 通过 nPgSQL 接收 NOTIFY 负载

java - Google Drive 在我的 Android 应用程序中返回错误 400

java - android的设计问题

c# - 使用 LINQ GroupBy 获取忽略属性的唯一集合

c# - Entity Framework .First() 缓存查询数据

c# - 如何检查数据库是否存在?

C# XML 反序列化和数组

xml - 使用shell脚本从xml标签获取属性值并转换为csv

c# - 反序列化 XML 时绕过特定父节点?

java - XStream - 解码 - XML 中指定的类型不可见