C# 使用带有私有和公共常量属性的 XmlSerializer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/429380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:01:03  来源:igfitidea点击:

Using XmlSerializer with private and public const properties

c#xml-serialization

提问by Adam Haile

What's the simplest way to get XmlSerializer to also serialize private and "public const" properties of a class or struct? Right not all it will output for me is things that are only public. Making it private or adding const is causing the values to not be serialized.

让 XmlSerializer 也序列化类或结构的私有和“公共常量”属性的最简单方法是什么?对我来说,它输出的并不是所有的东西都是公开的。将其设为私有或添加 const 会导致值未序列化。

采纳答案by Kent Boogaart

XmlSerializeronly looks at public fields and properties. If you need more control, you can implement IXmlSerializableand serialize whatever you would like. Of course, serializing a constant doesn't make much sense since you can't deserialize to a constant.

XmlSerializer只查看公共字段和属性。如果您需要更多控制,您可以实现IXmlSerializable并序列化您想要的任何内容。当然,序列化一个常量没有多大意义,因为你不能反序列化为一个常量。

回答by Hermit

Check out DataContractSerializer, introduced in .NET 3.0. It also uses XML format, and in many ways, it is better than XmlSerializer, including dealing with private data. See http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/for a full comparison.

查看 .NET 3.0 中引入的 DataContractSerializer。它还使用了 XML 格式,并且在很多方面都比 XmlSerializer 更好,包括处理私有数据。有关完整比较,请参阅http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

If you only have .NET 2.0, there's the BinarySerializer that can deal with private data, but of course it's a binary format.

如果您只有 .NET 2.0,则可以使用 BinarySerializer 来处理私有数据,当然它是一种二进制格式。

回答by Marc Gravell

It doesn't make sense to consider constmembers, as they aren't per-instance; but if you just mean non-public instance members: consider DataContractSerializer(.NET 3.0) - this is similar to XmlSerializer, but can serialize non-public properties (although it is "opt in").

考虑const成员是没有意义的,因为它们不是每个实例;但如果您只是指非公共实例成员:请考虑DataContractSerializer(.NET 3.0) - 这类似于XmlSerializer,但可以序列化非公共属性(尽管它是“选择加入”)。

See here for more.

请参阅此处了解更多信息

回答by Thomas Levesque

Even though it's not possible to serialize private properties, you can serialize properties with an internal setter, like this one :

即使无法序列化私有属性,您也可以使用内部 setter 序列化属性,如下所示:

public string Foo { get; internal set; }

To do that, you need to pre-generate the serialization assembly with sgen.exe, and declare this assembly as friend :

为此,您需要使用 sgen.exe 预先生成序列化程序集,并将此程序集声明为friend:

[assembly:InternalsVisibleTo("MyAssembly.XmlSerializers")]

回答by Flo

One other solution the use of Newtonsoft.Json:

使用 Newtonsoft.Json 的另一种解决方案:

   var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { root = result });
   var xml = (XmlDocument)Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json);

Sure, this one has unfortunately detour via json.

当然,不幸的是,这个通过json绕道而行。

回答by Chuck Bevitt

Here's my solution to putting immutable values in a property that will serialize to XML:

这是我将不可变值放在将序列化为 XML 的属性中的解决方案:

[XmlElement]
public string format { get { return "Acme Order Detail XML v3.4.5"; } set { } }