简体   繁体   中英

Deserialization XML to object with lists

I am trying to deserialize xml into an object but it doesn't go through the xml properly. It doesn't populate the authors in the object. I am trying to return an object that consists of articles containing a title and a list of authors. The list of authors won't populate in this code which is my issue. please help as I am new to this XML manipulation.

Here, you can see the issue. 在此输入图像描述

Here is the sample 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>

Here is my class hierarchy.

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

And here is my deserialization code.

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

This part:

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

Indicates that the serializer treats every <AuthorList> element as an author, instead of the extra <Author> level in your xml.

This can be solved this way:

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

PS. You can easily see what the serializer makes of your current serialization mapping by generating a MedlineCitationSet in code and serializing it.

您应该使用[XmlType(“MedlineCitationSet”)] [XmlType(“Article”)]和[XmlType(“Author”)]作为类中的属性,我发现至少缺少类Author中的xml定义。

尝试将XmlArrayItem属性添加到AuthorList属性。

Try to pt the xmlroot attributs on the author class and try again. You can remove ail attribute décaissé of the fact that xmlserializer May bé automatic.

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