我试图将xml反序列化为一个对象,但它没有正确地通过xml。它不会填充对象中的作者。我试图返回一个由包含标题和作者列表的文章组成的对象。作者列表不会填入此代码,这是我的问题。请帮忙,因为我是这个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>
这是我的班级层次结构。
[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);
}
答案 0 :(得分:3)
这部分:
[XmlElement("AuthorList")]
public List<Author> AuthorList { get; set; }
表示序列化程序将每个<AuthorList>
元素视为作者,而不是xml中的额外<Author>
级别。
这可以通过这种方式解决:
[XmlArray("AuthorList")]
[XmlArrayItem("Author")]
public List<Author> AuthorList { get; set; }
PS。通过在代码中生成MedlineCitationSet
并对其进行序列化,您可以轻松查看序列化程序对当前序列化映射的影响。
答案 1 :(得分:0)
你应该使用[XmlType(&#34; MedlineCitationSet&#34;)] [XmlType(&#34; Article&#34;)]和[XmlType(&#34; Author&#34;)]作为你的属性类,我看到至少缺少类Author中的xml定义。
答案 2 :(得分:0)
尝试将 2016-10-31T19:06:58.590888Z 0 [Note] Giving 0 client threads a chance to die gracefully
2016-10-31T19:06:58.591364Z 0 [Note] Shutting down slave threads
2016-10-31T19:06:58.592730Z 0 [Note] Forcefully disconnecting 0 remaining clients
2016-10-31T19:06:58.592792Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2016-10-31T19:06:58.598944Z 0 [Note] Binlog end
2016-10-31T19:06:58.701931Z 0 [Note] Shutting down plugin 'validate_password'
2016-10-31T19:06:58.705709Z 0 [Note] Shutting down plugin 'ngram'
2016-10-31T19:06:58.705751Z 0 [Note] Shutting down plugin 'ARCHIVE'
2016-10-31T19:06:58.705790Z 0 [Note] Shutting down plugin 'partition'
2016-10-31T19:06:58.705812Z 0 [Note] Shutting down plugin 'BLACKHOLE'
2016-10-31T19:06:58.705844Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
2016-10-31T19:06:58.706964Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2016-10-31T19:06:58.707001Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2016-10-31T19:06:58.707020Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2016-10-31T19:06:58.707037Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2016-10-31T19:06:58.707054Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2016-10-31T19:06:58.707208Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2016-10-31T19:06:58.707235Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2016-10-31T19:06:58.707255Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2016-10-31T19:06:58.707271Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2016-10-31T19:06:58.707286Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2016-10-31T19:06:58.707306Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2016-10-31T19:06:58.707322Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2016-10-31T19:06:58.707340Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2016-10-31T19:06:58.707358Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2016-10-31T19:06:58.707411Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2016-10-31T19:06:58.707432Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
2016-10-31T19:06:58.707449Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
2016-10-31T19:06:58.707467Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2016-10-31T19:06:58.707484Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2016-10-31T19:06:58.707499Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2016-10-31T19:06:58.707516Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2016-10-31T19:06:58.707534Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2016-10-31T19:06:58.707566Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2016-10-31T19:06:58.707587Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
2016-10-31T19:06:58.707603Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2016-10-31T19:06:58.707619Z 0 [Note] Shutting down plugin 'INNODB_CMP'
2016-10-31T19:06:58.707636Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2016-10-31T19:06:58.707654Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
2016-10-31T19:06:58.707672Z 0 [Note] Shutting down plugin 'INNODB_TRX'
2016-10-31T19:06:58.707698Z 0 [Note] Shutting down plugin 'InnoDB'
2016-10-31T19:06:58.711649Z 0 [Note] InnoDB: FTS optimize thread exiting.
2016-10-31T19:06:58.714623Z 0 [Note] InnoDB: Starting shutdown...
2016-10-31T19:06:58.819989Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2016-10-31T19:06:58.820088Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
2016-10-31T19:06:58.820228Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
2016-10-31T19:06:58.820416Z 0 [ERROR] InnoDB: Cannot open '/var/lib/mysql/ib_buffer_pool.incomplete' for writing: Permission denied
2016-10-31T19:07:00.692529Z 0 [Note] InnoDB: Shutdown completed; log sequence number 1333673446
2016-10-31T19:07:00.693394Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2016-10-31T19:07:00.693505Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
2016-10-31T19:07:00.693546Z 0 [Note] Shutting down plugin 'MEMORY'
2016-10-31T19:07:00.693586Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2016-10-31T19:07:00.693933Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
2016-10-31T19:07:00.695547Z 0 [Note] Shutting down plugin 'MyISAM'
2016-10-31T19:07:00.696705Z 0 [Note] Shutting down plugin 'CSV'
2016-10-31T19:07:00.696747Z 0 [Note] Shutting down plugin 'sha256_password'
2016-10-31T19:07:00.696758Z 0 [Note] Shutting down plugin 'mysql_native_password'
2016-10-31T19:07:00.696766Z 0 [Note] Shutting down plugin 'keyring_file'
2016-10-31T19:07:00.728590Z 0 [Note] Shutting down plugin 'binlog'
2016-10-31T19:07:00.737815Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
- 属性添加到XmlArrayItem
属性。
答案 3 :(得分:0)
尝试在作者类上查看xmlroot attributs,然后重试。您可以删除xmlserializer可能自动生成的事实的所有属性。