什么决定 CLR 用户定义类型的字段是否可 XML 序列化?

发布于 2024-11-30 19:36:32 字数 523 浏览 0 评论 0原文

作为我的第一个 .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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2024-12-07 19:36:32

原始 .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

×眷恋的温暖 2024-12-07 19:36:32

默认情况下,任何类型都被视为 XmlSerialized。您不必添加任何特别的东西。您只需要遵循一些规则:

  1. 您的类型必须定义默认构造函数。
  2. 您不能使用某些无法序列化为 XML 的对象(例如 IDictionary 派生对象)。
  3. 如果必须放置只读属性或 XML 序列化不支持的类型的属性,则必须使用 [XmlIgnore] 标记它们属性,因此它们不会被序列化。

如果您希望对类的序列化方式进行更多控制(例如,为节点提供特殊名称、控制类的属性是否序列化为节点或属性等),那么您应该查看这些属性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:

  1. Your type must define a default constructor.
  2. You must not use certain object, that can't be serialized to XML (like IDictionary derived objects.
  3. If you have to place readonly properties, or properties of types that are not supported by XML serialization, you have to mark them with [XmlIgnore] attributes, so they will not be serialized.

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

油饼 2024-12-07 19:36:32

来自“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文