强制XmlDefaultValue值的XML序列化 [英] Force XML serialization of XmlDefaultValue values

查看:182
本文介绍了强制XmlDefaultValue值的XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用从XSD文档生成的C#类,我可以创建一个对象,并将其成功序列化。但是,某些属性定义了XmlDefaultValue。如果有任何对象具有默认值,则序列化对象时不会创建这些属性。

Using a C# class generated from an XSD document, I can create an object, and serialize it successfully. However, some attributes have an XmlDefaultValue defined. If any objects have the default value, then those attributes do not get created when the object is serialized.

根据文档。但这不是我希望它表现的方式。我需要在XML文档中生成所有这些属性。

我检查了所有可应用的代码属性,即使它们是默认值,也可能会强制将其输出。找不到类似的东西。

This is expected behavior according to the documentation. But this is not how I want it to behave. I need to have all such attributes generated in the XML document.
I've checked for any code attributes that can be applied that might force it to be outputted, even if it is the default value, but I couldn't find anything like that.

有什么方法可以使该工作正常吗?

Is there any way to make that work?

推荐答案

我找到了答案:
https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.emitdefaultvalue%28v=vs.110%29.aspx

在DataContract中设置属性,如下所示:[DataMember(EmitDefaultValue = true)]

Set the attribute in the DataContract like this: [DataMember(EmitDefaultValue=true)]

这篇关于强制XmlDefaultValue值的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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