datacontractserializer

How to serialize / deserialize immutable list type in c#

时光总嘲笑我的痴心妄想 提交于 2019-11-28 00:51:20
问题 If I have a class defined [DataContract()] class MyObject { [DataMember()] ImmutableList<string> Strings { get; private set} } The ImmutableList<T> type comes from the immutables library https://www.nuget.org/packages/Microsoft.Bcl.Immutable. Note that the class ImmutableList does not have a default constructor or a mutable Add method. Adding things to the list take the form. myList = myList.Add("new string"); Can I add some custom support to the .NET serialization mechanism to support this

Custom serialization with DataContractSerializer

两盒软妹~` 提交于 2019-11-27 23:52:27
问题 I'm currently using wrapper classes for my DataSets ,in order to implement custom serialization. I would like to use DataContractSerializer (more like have to use it) but still support the custom serialization. The problem is that the [DataContract] and [Serializable] attributes don't seem to get along so well... how could I override the serialization, and support BOTH DataContract & ISerializable serialization? The code for the wrapper DataSet class is brought here: [Serializable()] [System

Ignore field order in DataContractSerializer

坚强是说给别人听的谎言 提交于 2019-11-27 23:27:07
When deserializing, DataContractSerializer requires not only that an element name matches, but also that it is in a certain order with respect to the other elements. My application is such that every field can be uniquely identified by its name. I would therefore like it to be possible for the XML file to contain the elements in any order and for the deserializer to still work. Is it possible to set up a DataContract like this? The introductory paragraph in Data Member Order suggests that order is enforced optionally , but I haven't found a way to actually make it optional. Follow-up question

Serialize/deserialize objects - order of fields matters?

拟墨画扇 提交于 2019-11-27 18:36:15
问题 Is it possible that DataContractSerializer wrongly deserializes an object if the fields are not in the "correct" (whatever that means) order? The classes that I try to serialize/deserialize do not have order-attributes placed on fields/properties. Yet one of my fields always gets deserialized as null even though it has a non-null value (it actually contains a list of strings). When I moved two XML elements in serialized file around to match the order in another XML example (for which

WCF chokes on properties with no “set ”. Any workaround?

守給你的承諾、 提交于 2019-11-27 17:55:28
I have some class that I'm passing as a result of a service method, and that class has a get-only property: [DataContract] public class ErrorBase { [DataMember] public virtual string Message { get { return ""; } } } I'm getting an exception on service side: System.Runtime.Serialization.InvalidDataContractException: No set method for property 'Message' in type 'MyNamespace.ErrorBase'. I have to have this property as only getter, I can't allow users to assign it a value. Any workaround I could use? Or am I missing some additional attribute? Give Message a public getter but protected setter, so

DataContractSerializer is an opt-in serializer. How to make it Opt-Out?

时间秒杀一切 提交于 2019-11-27 16:27:23
I inherited some classes with a large number of attributes. I want to be able to serialize them for WCF . As DataContractSerializer is an opt-in serializer, I will need to decorate all the properties with DataMember attribute, which seems to me a little cumbersome. Don't I have any other way around so that I don't have to add DataMember to all the properties? Please note, most of my properties need to be serialized. Mathieson It depends if your properties can be automatically generated - if so, you could simple substitute the WCF serializer. Usually this is done to use JSON.NET instead: C# WCF

DataContractSerializer: How to serialize classes/members without DataContract/DataMember attributes

青春壹個敷衍的年華 提交于 2019-11-27 16:25:41
问题 DataContractSerializer requires classes and members to be marked with the DataContract and DataMember attributes. However, in my case the classes are auto-generated with the EFPocoAdapater framework and these attributes are not present. How can I force serialization of all members using the DataContractSerializer without these attributes being present? From Alexdej: This changed in 3.5SP1, hope you saw that: http://www.pluralsight.com/community/blogs/aaron/archive/2008/05/13/50934.aspx 回答1:

Is there a way to make DataContractSerializer output cleaner XML?

六眼飞鱼酱① 提交于 2019-11-27 15:06:45
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

Using DataContractSerializer to serialize, but can't deserialize back

谁说胖子不能爱 提交于 2019-11-27 10:29:20
I have the following 2 functions: public static string Serialize(object obj) { DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); MemoryStream memoryStream = new MemoryStream(); serializer.WriteObject(memoryStream, obj); return Encoding.UTF8.GetString(memoryStream.GetBuffer()); } public static object Deserialize(string xml, Type toType) { MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)); // memoryStream.Position = 0L; XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, new XmlDictionaryReaderQuotas(

Cloning objects without Serialization

早过忘川 提交于 2019-11-27 09:43:22
I've found numerous solutions here at SO and elsewere that deal with deep clone of object via serialization/deserialization (into memory and back). It requires that classes to be cloned are marked with [Serializable] . I happen to have my classes (well most of them) marked with [DataContract] because I use DataContractSerializer to serialize into XML. I only introduced [Serializable] attribute because of the need for deep clone of some of these class instances. However, now something happened to serialization/deserialization via the DCS because it does not work anymore - errors about expecting