什么决定 CLR 用户定义类型的字段是否可 XML 序列化?
作为我的第一个 .NET 项目之一,我尝试创建一个 CLR 用户定义类型以在 SQL Server 中使用。在 MSDN Library 的 实施 UDT 的要求中,它说:
UDT 必须实现 System.Xml.Serialization.IXmlSerializable,或者所有公共字段和属性都必须是 XML 可序列化的类型,或者如果需要覆盖标准序列化,则必须使用 XmlIgnore 属性进行修饰。
什么规则决定我的字段和属性是否可 XML 序列化(使用标准 XML 序列化)?哪些类型需要自定义 XML 序列化?
编辑:最终,我想知道是否必须实现 IXmlSerialized
接口。听起来我可以选择要么实现它,要么将自己限制为 SQL Server CLR 可以自动处理的类/结构成员。
As one of my first .NET projects, I'm trying to create a CLR user-defined type for use in SQL Server. In MSDN Library's Requirements for Implementing UDTs it says:
The UDT must implement System.Xml.Serialization.IXmlSerializable, or all public fields and properties must be of types that are XML serializable or decorated with the XmlIgnore attribute if overriding standard serialization is required.
What rule determines whether or not my fields and properties are XML serializable (using standard XML serialization)? What types require custom XML serialization?
Edit: Ultimately, I want to know whether or not I have to implement the IXmlSerializable
interface. It sounds like I have a choice to either implement it or limit myself to class/struct members that SQL Server CLR can handle automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原始 .Net 类型(如 string、DateTime、int、boolean 等)是 XmlSerialized,如果您的自定义类型的属性是 MyClass,则此类型应该是可序列化的,用 序列化属性
Primitive .Net types like string, DateTime, int, boolean etc. are XmlSerializable, if the property of your custom type f.i. MyClass this type should be serializable, marked with serilization attributes
默认情况下,任何类型都被视为 XmlSerialized。您不必添加任何特别的东西。您只需要遵循一些规则:
如果您希望对类的序列化方式进行更多控制(例如,为节点提供特殊名称、控制类的属性是否序列化为节点或属性等),那么您应该查看这些属性http://msdn.microsoft.com/en-us/library/83y7df3e.aspx
Any type is considered XmlSerializable by default. You don't have to add anything special. You just need to follow some rules:
If you want more control on the way your class is serialized (e.g. provide special names for nodes, control whether properties of your classes are serialized as nodes or attributes, etc), then you should look at these attributes http://msdn.microsoft.com/en-us/library/83y7df3e.aspx
来自“XML 序列化简介”:
可序列化的项目
以下项目可以使用 XmLSerializer 类进行序列化:
公共类的公共读/写属性和字段。
实现 ICollection 或 IEnumerable 的类。
注意:
仅序列化集合,而不序列化公共属性。
XmlElement 对象。
XmlNode 对象。
DataSet 对象。
From "Introducing XML Serialization":
Items That Can Be Serialized
The following items can be serialized using the XmLSerializer class:
Public read/write properties and fields of public classes.
Classes that implement ICollection or IEnumerable.
Note:
Only collections are serialized, not public properties.
XmlElement objects.
XmlNode objects.
DataSet objects.