- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个自定义类型的集合属性,它继承自 BindingList。目前,此属性通过 XmlSerializer 进行序列化,即使它没有 Setter。我现在正尝试在此自定义集合类上实现 IXmlSerializable,并发现只有在我的集合属性具有 Setter 时才会调用 WriteXml() 和 ReadXml() 接口(interface)方法。为什么序列化现在忽略此属性,除非在没有 Setter 的情况下正确序列化之前存在 Setter。
重现:
首先,有一个名为“Item”的类:
public class Item
{
public Item() {}
// Generates some random data for the collection
private MyCollectionType GenerateContent()
{
Random ranGen = new Random();
MyCollectionType collection = new MyCollectionType();
for (int i = 0; i < 5; i ++)
{
collection.Add("Item" + ranGen.Next(0,101));
}
return collection;
}
public MyCollectionType Items
{
get
{
if (m_Items == null)
{
m_Items = GenerateContent();
}
return m_Items;
}
}
private MyCollectionType m_Items = null;
}
接下来创建集合类“MyCollectionType”(请注意,IXmlSerializable 在开头的代码片段中故意缺失):
public class MyCollectionType : BindingList<string>
{
public MyCollectionType()
{
this.ListChanged += MyCollectionType_ListChanged;
}
void MyCollectionType_ListChanged(object sender, ListChangedEventArgs e){ }
public MyCollectionType(ICollection<string> p)
{
this.ListChanged += MyCollectionType_ListChanged;
}
#region Implementation of IXmlSerializable
public void WriteXml(XmlWriter writer)
{
throw new NotImplementedException();
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
public XmlSchema GetSchema() { return null; }
#endregion
}
最后,在 Main() 中添加一些代码来序列化和反序列化“Item”:
Item myItem = new Item();
Item newItem = null;
// Define an XmlSerializer
XmlSerializer ser = new XmlSerializer(typeof(Item));
// Serialize the Object
using (TextWriter writer = File.CreateText(@"c:\temporary\exportedfromtest.xml"))
{
ser.Serialize(writer,myItem);
}
// Deserialize it
using (Stream reader = new FileStream(@"c:\temporary\exportedfromtest.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (XmlDictionaryReader xmlDictionaryReader = XmlDictionaryReader.CreateTextReader(reader, XmlDictionaryReaderQuotas.Max))
{
newItem = (Item)ser.Deserialize(xmlDictionaryReader);
}
}
因此,如果您按原样运行它,您应该会看到它在没有 Setter 的情况下进行序列化和反序列化。目前,该集合没有在上面的代码片段中列出“IXmlSerializable”,但方法在那里。因此,如果您现在返回并将“IXmlSerializable”添加到 MyCollectionType 类并再次运行,您会注意到集合属性未序列化,并且未调用 WriteXml() 和 ReadXml() 方法。另请注意,如果您添加一个空的 Setter,这些方法将突然被调用。
最佳答案
这是记录在案的行为。在 Introducing XML Serialization 中对此进行了解释,尽管不清楚。 :
XML serialization does not convert methods, indexers, private fields, or read-only properties (except read-only collections). To serialize all an object's fields and properties, both public and private, use the DataContractSerializer instead of XML serialization.
如您所见,只读属性通常不会序列化——只读集合除外。但微软的意思是什么?集合毕竟不是属性(property)。
他们的意思如下:
XML 序列化不转换只读属性或只读字段(具有预初始化集合的只读集合值属性除外)。
(顺便说一句,这意味着,如果您在包含类型的构造函数中将项目添加到您的集合,然后对其进行序列化和反序列化,则默认项目将复制。有关原因的解释,请参见XML Deserialization of collection property with code defaults。如果我反序列化您的 Item
类,我会看到这种行为。)
这如何适用于您的情况?好吧,当您使集合实现 IXmlSerializable
时,序列化程序不再将其解释为集合;它将它解释为一个黑盒子。因此,它的特殊收藏规则不再适用。引用您的集合的属性现在必须是可读/可写的; XmlSerializer
将自己构造一个实例,对其进行反序列化,并将其设置到其父级中,就像任何其他非集合属性值一样。
关于c# - 实现 IXmlSerializable 需要集合属性具有 Setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15662277/
我需要能够对内部类进行 Xml 序列化,因此我必须实现 IXmlSerializable。 这个类有两个字符串和一个列表。 我知道使用 WriteElementString 和 ReadElement
我有一个看起来像这样的 xml 文件: value value value value 我有两个对象代表这个 xml: class
.NET 中的 XML 序列化通过 XmlSerializer 构造函数的 extraTypes[] 参数允许多态对象。它还允许为实现 IXmlSerializable 的类型自定义 XML 序列化。
我正在实现 IXmlSerializable对于我的类的自定义(反)序列化逻辑,但希望根据 XSD 模式检查 XML 的读写。我添加了 XmlSchemaProviderAttribute : [Xm
假设我有两个类,一个基类和一个派生类。它们是相当简单的类,主要充当数据结构(派生类显然稍微复杂一些)。 public class BaseUserSession { // ... Variou
我试图创建一个通用的 Dictionary实现 IXmlSerializable (归功于 Charles Feduke )。 这是我的试验: Sub Main() Dim z As New
我正在构建一个将数据公开为 XML 的 REST API。我的域层中有一大堆域类,供 API 后面的服务层和我们将提供给客户的客户端 API 使用。 (客户确实可以选择直接与 REST API 交互,
是否有任何标记为 IXmlSerializable 的 .NET 通用集合?我已经尝试过 List 和 Collection,但都没有开箱即用。 在我推出自己的 collection、list 或 d
我对 xml 序列化有一个奇怪的要求。 请引用以下 C# 代码(由于变量 'rootName' 超出范围而无法编译)。我的意图是让我的类(class) GeneralData 成为“一般”。这意味着该
我有一个类: [Serializable] public class Profile { [XmlAttribute] private string[] permissions;
我想对我的对象 Exception 的实例进行 XML 序列化,并将其存储在另一个对象 ExceptionReport 的 XMLNode[] Nodes 属性中。 [System.Diagnosti
我有一个实现 IXmlSerializable 的类,如下所示: public class SomeClass : IXmlSerializable { public SomeSerializ
一旦程序员决定实现IXmlSerializable,实现它的规则和最佳实践是什么?我听说 GetSchema() 应该返回 null 并且 ReadXml 应该在返回之前移动到下一个元素。这是真的?那
我有这样的 xml: 2 6,501698000000 8,414278000000 9,29
对于要使用的正确装饰器或可能需要的任何设计有点困惑。当序列化一个实现 IXmlSerializable 的类时,有没有办法在 XmlRoot 元素中包含命名空间及其前缀? 例如类定义。 using S
我有以下类定义 [XmlRoot(ElementName = "person",Namespace = "MyNamespace")] public class Person : IXmlSerial
我有一个自定义类型的集合属性,它继承自 BindingList。目前,此属性通过 XmlSerializer 进行序列化,即使它没有 Setter。我现在正尝试在此自定义集合类上实现 IXmlSeri
初始问题 我要serialize一个List更改 IXmlSerializable 的 XmlType动态分类(所以我不能使用属性标签来做到这一点) 我已经尝试使用 XmlAttributeOverr
我发现 XmlSerializer 有一些有趣的行为。 如果我尝试序列化一个具有 Type 对象属性并且该对象实现了 IXmlSerializable 的类,序列化程序将抛出一个 InvalidOpe
基本上,最初的问题是我需要将 bool 值序列化为 0 或 1。我找到的解决方案是实现 IXmlSerializable,我确实这样做了。不幸的是,我试图序列化的类是从模式生成的代码,并且上面有一个
我是一名优秀的程序员,十分优秀!