有没有一种方法,使DataContractSerializer的输出清洁XML? [英] Is there a way to make DataContractSerializer output cleaner XML?

查看:158
本文介绍了有没有一种方法,使DataContractSerializer的输出清洁XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用DataContractSerializer的序列化我的对象,我得到类似于

Using the DataContractSerializer to serialize my object I get an output similar to

 <?xml version="1.0" encoding="utf-8" ?> 
 <AgentNotification xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/The.name.space.Notifications">
  <_x003C_Created_x003E_k__BackingField i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/The.name.space" /> 
  <_x003C_Id_x003E_k__BackingField i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/The.name.space" />        
 <_x003C_Email_x003E_k__BackingField>some@email.com</_x003C_Email_x003E_k__BackingField> 
  <_x003C_Name_x003E_k__BackingField>Random Person</_x003C_Name_x003E_k__BackingField> 
 <_x003C_Policies_x003E_k__BackingField>
 <PolicyNotification>
  <_x003C_Created_x003E_k__BackingField i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/The.name.space" /> 
  <_x003C_Id_x003E_k__BackingField i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/The.name.space" /> 
  <_x003C_ConfirmationNumber_x003E_k__BackingField>Some number</_x003C_ConfirmationNumber_x003E_k__BackingField>   
  </PolicyNotification>
 <PolicyNotification>
  </_x003C_Policies_x003E_k__BackingField>  
  </AgentNotification>



有没有办法为它的只是

Is there any way for it to output tags that are just

<Id>
<Name>



等,而不需要覆盖的属性我的班?

etc, without the need to cover my classes with attributes?

如果有没有一种方法,它的输出是保证每次都一样正确吗?所以,如果我用这个来呈现我的对象图是用XML文件生成一个X *文件,我永远也不会碰到在我的节点更改名称和文件出来一个问题混搭空白正确吗?

If there's not a way the output of this is guaranteed to be the same every time correct? So that if I use this to render my object graphs are XML to mash up with an X* document for file generation that I'll never run into an issue where my nodes change names and the document comes out blank correct?

推荐答案

这是发生,因为你必须标记你的类型(如 AgentNotification )与< A HREF =htt​​ps://msdn.microsoft.com/en-us/library/system.serializableattribute%28v=vs.110%29.aspx> [Serializable接口] 。当的DataContractSerializer 遇到标记 A型[Serializable接口] ,但没有明确的 [DataContract] ,它生成匹配类型如何合同 < A HREF =htt​​ps://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter%28v=vs.110%29.aspx> 的BinaryFormatter 连载一类,这是连载的一类的所有成员变量 - 甚至变量标记为私人 - 按名称。对于自动实现属性的,这意味着的秘密支持字段通过名字来取得序列;他们的名字是你所看到的奇特元素名称。

This is happening because you must have marked your types (e.g. AgentNotification) with [Serializable]. When DataContractSerializer encounters a type marked with [Serializable] but no explicit [DataContract], it generates a default contract for the type that matches how BinaryFormatter serializes a class, which is to serialize all member variables of a class — even variables marked as private — by name. For auto-implemented properties, this means the secret backing fields get serialized by name; their names are the peculiar element names you are seeing.

要解决这个问题最简单的方法是删除 [Serializable接口] 从你的类属性。你几乎可以肯定,除非你实际使用的是不需要它的BinaryFormatter SoapFormatter 。做如此,的DataContractSerializer 现在序列化公共属性和名称字段,而不是按名称的公共和私人领域。

The easiest way to solve this is to remove the [Serializable] attribute from your classes. You almost certainly don't need it unless you are actually using BinaryFormatter or SoapFormatter. Having done so, DataContractSerializer will now serialize your public properties and fields by name, rather than public and private fields by name.

这篇关于有没有一种方法,使DataContractSerializer的输出清洁XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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