Xsd.exe 生成的类在 null 值时不会序列化必需元素 [英] Xsd.exe generated class doesn't serialize mandatory elements when null value

查看:23
本文介绍了Xsd.exe 生成的类在 null 值时不会序列化必需元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下 XSD 中,所有元素都是必需的:

In the following XSD all elements are mandatory:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://TestNamespace" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://TestNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Test">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="1" name="Id" type="xs:int" />
        <xs:element minOccurs="1" maxOccurs="1" name="EMail" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

但是,当我序列化 xsd.exe 生成的类的实例时,其中 EMail == null,根据架构,生成的 XML 是无效的,因为 EMail 元素完全丢失.

However, when I serialize an instance of the xsd.exe generated class where EMail == null, the resulting XML is invalid according to the schema, because the EMail element is missing altogether.

<?xml version="1.0"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://TestNamespace">
  <Id xmlns="">2</Id>
</Test>

这是为什么?有什么办法可以防止吗?

Why is that? Is there any way to prevent it?

推荐答案

在您的评论中,您提到您希望将空值用作缺失的 Email 元素的默认值.

In your comments you mention that you want an empty value to be used as a default for a missing Email element.

这是架构级别的问题,与序列化程序或代理生成器无关.

This is a schema-level problem, not related to the serializer or the proxy generator.

这种行为应该在模式中定义,而不是留给序列化器.不幸的是,XML Schema 不允许默认元素值,因为它太远了太复杂了,无法猜测默认元素的结构是什么.它本身是否具有必需的元素?数组?选择元素呢?

Such behaviour should be defined in the schema, not left to the serializer. Unfortunately, XML Schema doesn't allow default element values because it's far too complex to guess what the default element's structure would be. Does it have required elements itself? Arrays? What about choice elements?

XML Schema 确实允许默认或固定的属性值 允许 不过,使用 defaultfixed 属性.

XML Schema does allow Default or fixed attribute values are allowed though, using the default and fixed attributes.

一种解决方案是创建一个您认为有效的空 Email 对象的实例,并使用它而不是空值.

One solution is to create an instance of what you consider a valid empty Email object and use it instead of null values.

static readonly EmptyEmail=new Email();
...
test.Email=test.Email??EmptyEmail;

事实上,这是空对象模式的一种情况——使用空对象实例而不是空值.许多 .NET Framework 类将 Null 对象存储为类本身上称为 Empty 的静态只读字段.这使得代码更简洁:

In fact, this is one case of the Null Object pattern - use a Null Object instance instead of null values. A lot of .NET Framework classes store the Null object as a static readonly field called Empty on the class itself. This makes for a bit cleaner code:

test.Email=test.Email??Email.Empty;

另一种选择是在类的构造函数中初始化 Email 属性.XSD 生成的类是部分的,它允许您在重新生成代理时不会被覆盖的部分文件中定义构造函数,例如:

Another option is to initialize the Email property in the class's constructor. XSD generated classes are partial which allows you to define a constructor in a partial file that won't be overwritten when you regenerate the proxy, eg:

public partial class Test 
{
    public Test()
    {
        Email=new Email();
    }

}

这篇关于Xsd.exe 生成的类在 null 值时不会序列化必需元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆