使用列表反序列化 XML 到 object

Deserialization XML to object with lists

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

在这里,您可以看到问题所在。

这是示例 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>

这是我的 class 层次结构。

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

您应该使用 [XmlType("MedlineCitationSet")] [XmlType("Article")] 和 [XmlType("Author")] 作为 classes 中的属性,我看到至少缺少 class 作者中的 xml 定义。

尝试向 AuthorList 属性 添加 XmlArrayItem 属性。

这部分:

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

表示序列化程序将每个 <AuthorList> 元素视为作者,而不是 xml.

中额外的 <Author> 级别

可以这样解决:

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

PS。通过在代码中生成 MedlineCitationSet 并对其进行序列化,您可以很容易地看到序列化程序对当前序列化映射做了什么。

尝试将 xml 根属性放在作者 class 上,然后重试。您可以删除 xmlserializer 可能是自动的事实的所有属性 décaissé。