DataContractSerializer 生成没有子节点的 XML,不保留任何数据
DataContractSerializer produces XML with no child nodes, preserves no data
我一直在阅读 Whosebug,但最终 运行 阅读了互联网似乎没有现成答案的内容。在发现 XMLSerializer 无法序列化字典后,我切换到 DataContractSerializer。问题是,无论我如何处理,序列化都会悄悄失败。这是我要序列化的内容。它是高度简化的(我试图缩小问题的范围),但与更复杂的前辈一样患有同样的疾病:
[DataContract(Namespace = "")]
public class TestData
{
public Dictionary<Enum1, List<string>> List1 = new Dictionary<Enum1, List<string>>();
public DateTime WeekStarting { get; private set; }
public DateTime LastSaved { get; set; }
public int Iteration { get; set; }
public string FilePath { get; private set; }
public string FileName { get; private set; }
public TestData()
{
FilePath = "C:/";
FileName = "whatever.xml";
foreach (var category in Enum.GetValues(typeof(Enum1)).Cast<Enum1>())
{
List1.Add(category, new List<string>());
}
LastSaved = DateTime.Now;
WeekStarting = DateTime.Now;
Iteration = 0;
}
}
我最初尝试使用此代码对其进行序列化:
var serializer = new DataContractSerializer(typeof(TestData));
using (var writer = new XmlTextWriter(File.CreateText(Path.Combine(CurrentWeek.FilePath, CurrentWeek.FileName))))
{
writer.Formatting = Formatting.Indented;
serializer.WriteObject(writer, currentWeek);
}
在查看了有关 Whosebug 的类似问题(完全为空的输出,与我的不同)之后,我也试过这个:
var serializer = new DataContractSerializer(typeof(TestData));
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, CurrentWeek);
ms.Position = 0;
string serializedContent;
using (StreamReader sr = new StreamReader(ms))
{
serializedContent = sr.ReadToEnd();
}
还有这个:
var serializer = new DataContractSerializer(typeof(TestData));
using (var sw = new StringWriter())
{
using (var writer = new XmlTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
serializer.WriteObject(writer, CurrentWeek);
writer.Flush();
var xmlString = sw.ToString();
}
}
所有这些都产生了一个 XML 文件,仅包含以下内容:
<TestData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
...一个 XML 其中包含一些内容,但实际上是空的,因为它没有序列化任何 TestData 的内容。加载它并对其执行操作将导致我的程序中发生各种不好的事情。当它有一个名称空间时,事情并没有好转。有人有什么想法吗?
您需要用 [DataMember]
attribute. From the documentation for DataContractAttribute
标记要序列化的属性
Apply the DataContractAttribute attribute to types (classes, structures, or enumerations) that are used in serialization and deserialization operations by the DataContractSerializer.
You must also apply the DataMemberAttribute to any field, property, or event that holds values you want to serialize. By applying the DataContractAttribute, you explicitly enable the DataContractSerializer to serialize and deserialize the data.
另外,如果你想序列化你的 Dictionary<Enum1, List<string>>
,你需要用 [DataContract]
(可能用命名空间 [DataContract(Namespace="")]
)标记你的 enum Enum1
并且每个 enum
成员 [EnumMember]
. More here: Enumeration Types in Data Contracts.
我一直在阅读 Whosebug,但最终 运行 阅读了互联网似乎没有现成答案的内容。在发现 XMLSerializer 无法序列化字典后,我切换到 DataContractSerializer。问题是,无论我如何处理,序列化都会悄悄失败。这是我要序列化的内容。它是高度简化的(我试图缩小问题的范围),但与更复杂的前辈一样患有同样的疾病:
[DataContract(Namespace = "")]
public class TestData
{
public Dictionary<Enum1, List<string>> List1 = new Dictionary<Enum1, List<string>>();
public DateTime WeekStarting { get; private set; }
public DateTime LastSaved { get; set; }
public int Iteration { get; set; }
public string FilePath { get; private set; }
public string FileName { get; private set; }
public TestData()
{
FilePath = "C:/";
FileName = "whatever.xml";
foreach (var category in Enum.GetValues(typeof(Enum1)).Cast<Enum1>())
{
List1.Add(category, new List<string>());
}
LastSaved = DateTime.Now;
WeekStarting = DateTime.Now;
Iteration = 0;
}
}
我最初尝试使用此代码对其进行序列化:
var serializer = new DataContractSerializer(typeof(TestData));
using (var writer = new XmlTextWriter(File.CreateText(Path.Combine(CurrentWeek.FilePath, CurrentWeek.FileName))))
{
writer.Formatting = Formatting.Indented;
serializer.WriteObject(writer, currentWeek);
}
在查看了有关 Whosebug 的类似问题(完全为空的输出,与我的不同)之后,我也试过这个:
var serializer = new DataContractSerializer(typeof(TestData));
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, CurrentWeek);
ms.Position = 0;
string serializedContent;
using (StreamReader sr = new StreamReader(ms))
{
serializedContent = sr.ReadToEnd();
}
还有这个:
var serializer = new DataContractSerializer(typeof(TestData));
using (var sw = new StringWriter())
{
using (var writer = new XmlTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
serializer.WriteObject(writer, CurrentWeek);
writer.Flush();
var xmlString = sw.ToString();
}
}
所有这些都产生了一个 XML 文件,仅包含以下内容:
<TestData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
...一个 XML 其中包含一些内容,但实际上是空的,因为它没有序列化任何 TestData 的内容。加载它并对其执行操作将导致我的程序中发生各种不好的事情。当它有一个名称空间时,事情并没有好转。有人有什么想法吗?
您需要用 [DataMember]
attribute. From the documentation for DataContractAttribute
Apply the DataContractAttribute attribute to types (classes, structures, or enumerations) that are used in serialization and deserialization operations by the DataContractSerializer.
You must also apply the DataMemberAttribute to any field, property, or event that holds values you want to serialize. By applying the DataContractAttribute, you explicitly enable the DataContractSerializer to serialize and deserialize the data.
另外,如果你想序列化你的 Dictionary<Enum1, List<string>>
,你需要用 [DataContract]
(可能用命名空间 [DataContract(Namespace="")]
)标记你的 enum Enum1
并且每个 enum
成员 [EnumMember]
. More here: Enumeration Types in Data Contracts.