【发布时间】:2017-05-19 20:18:22
【问题描述】:
我在课堂上得到了这个:
namespace MSAToolsLibrary.PublisherEntry
{
[XmlRoot(ElementName = "PublisherDatabase", Namespace = "http://www.publictalksoftware.co.uk/msa")]
public class PublisherData
{
public PublisherData()
{
//_Publishers = new List<Publisher>();
_PublishersDictionary = new Dictionary<string, Publisher>();
}
public List<Publisher> Publishers
{
get { return _PublishersDictionary.Select(x => x.Value).ToList(); }
set { _PublishersDictionary = value.ToDictionary(x => x.Name, x => x); }
}
private Dictionary<string, Publisher> _PublishersDictionary;
[XmlIgnore]
public Dictionary<string, Publisher> PublisherDictionary
{
get { return _PublishersDictionary; }
}
public void AddPublisher(String strName, String strNotes, Gender eGender, Appointed eAppointedAs, Serving eServingAs, bool bUseForDemonstrations, bool bAvailableMidweek, bool bAvailableWeekend, DateTime[] listDatesNotAvailable)
{
Publisher _Publisher = new Publisher()
{
Name = strName,
Notes = strNotes,
Gender = eGender,
AppointedAs = eAppointedAs,
ServingAs = eServingAs,
};
_Publisher.Assignments.UseForDemonstrations = bUseForDemonstrations;
_Publisher.Availability.Midweek = bAvailableMidweek;
_Publisher.Availability.Weekend = bAvailableWeekend;
_Publisher.Availability.DatesNotAvailable = new List<DateTime>(listDatesNotAvailable);
//_Publishers.Add(_Publisher);
_PublishersDictionary.Add(strName, _Publisher);
}
}
}
现在,当我将数据保存到 XML 时,一切正常。
但是当我阅读时:
public void ReadPublisherData(out Int64 iResult)
{
_PublisherData.Publishers.Clear(); // Reset
iResult = MakeResult(true);
try
{
XmlSerializer x = new XmlSerializer(_PublisherData.GetType());
using (StreamReader reader = new StreamReader(_strPathXML))
{
_PublisherData = (PublisherData)x.Deserialize(reader);
iResult = _PublisherData.PublisherDictionary.Count;
}
}
catch
{
iResult = MakeResult(false);
}
}
不起作用。我在列表或字典中的发布者为零。
我做错了什么?
更新
如果我更改 PublisherData 声明以使其具有所需的后退字段:
public PublisherData()
{
_Publishers = new List<Publisher>();
_PublishersDictionary = new Dictionary<string, Publisher>();
}
public List<Publisher> Publishers
{
get => _Publishers; set => _Publishers = value;
}
private List<Publisher> _Publishers;
然后这会导致数据正确序列化,我得到了 MFC 应用程序中的预期结果。但现在我的PublisherDictionary 挂了。于是我加了一个函数:
public void BuildPublisherDictionary()
{
_PublishersDictionary = _Publishers.ToDictionary(x => x.Name, x => x);
}
并调整了读取例程:
public void ReadPublisherData(out Int64 iResult)
{
iResult = MakeResult(true);
try
{
_PublisherData.Publishers.Clear(); // Reset
XmlSerializer x = new XmlSerializer(_PublisherData.GetType());
using (StreamReader reader = new StreamReader(_strPathXML))
{
_PublisherData = (PublisherData)x.Deserialize(reader);
_PublisherData.BuildPublisherDictionary();
iResult = _PublisherData.PublisherDictionary.Count;
}
}
catch
{
iResult = MakeResult(false);
}
}
它有效。但我不知道这是否是最简单的方法。我对上面的问题是列表/字典现在彼此分离。因此,如果我将发布者添加到字典中,它现在不会出现在列表中。
更新 2
目前,在阅读 XML 之后,如果我添加或删除一个发布者,我会将它应用于列表和字典。除非有更简单的方法。
【问题讨论】:
-
首先用记事本检查 XML 文件,看看文本是否好看。看起来序列化程序正在添加命名空间,但没有添加阅读器。
-
您的 Publishers 属性取决于 Dictionary 但该 Dictionary 不会在反序列化时填充。为什么 Publishers 不能通过支持字段简单实现?
-
XML 文件很好,因为它是使用应用程序创建的。所以我知道它的结构是好的。
-
@rene 我不太明白。那它应该如何工作?我需要“Publishers”列表进行序列化,但我需要“PublishersDictionary”用于一般用途。我应该如何改变它?
-
@rene 按照我的理解,Publishers setter 将从值对象中填充字典。
标签: c# com xmlserializer