datacontractserializer

How to serialize / deserialize immutable list type in c#

时光总嘲笑我的痴心妄想 提交于 2019-11-29 07:16:12
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 type and show it how to deserialize it? Currently the collection is just skipped on deserialization

Set default value in a DataContract?

倖福魔咒の 提交于 2019-11-29 06:59:11
问题 How can I set a default value to a DataMember for example for the one shown below: I want to set ScanDevice="XeroxScan" by default [DataMember] public string ScanDevice { get; set; } 回答1: I've usually done this with a pattern like this: [DataContract] public class MyClass { [DataMember] public string ScanDevice { get; set; } public MyClass() { SetDefaults(); } [OnDeserializing] private void OnDeserializing(StreamingContext context) { SetDefaults(); } private void SetDefaults() { ScanDevice =

Custom serialization with DataContractSerializer

戏子无情 提交于 2019-11-29 06:27:58
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.Runtime.InteropServices.ComVisible(false)] public class TestDatasetWrapper : TestDataSet, ISerializable

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

喜欢而已 提交于 2019-11-29 02:05:58
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 You cannot - plain and simple. The attribute are needed for the DataContractSerializer to pick up which

How to serialize a derived type as its base type with WCF

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 21:33:12
I have a common library with some objects in it. Then I have a service project that references the common library and creates some derived types from objects in the common library. I want my service to serialize the derived types as their base types defined in the common library. I cannot use KnownTypes on the objects in the common library because I don't want the common library referencing the service assemblies. So how can I have wcf serialize the derived types as their base types? I wish I could do something like... [DataContract(SerializeAsType = typeof(BaseType))] public class DerivedType

Deserializing XML with DataContractSerializer

落花浮王杯 提交于 2019-11-28 20:39:46
I have a web service that returns the following data: <?xml version=""1.0"" encoding=""UTF-8""?> <RESPONSE> <KEY>12345</KEY> <PROPERTY> <PROPERTY_ADDRESS> <STREET_NUM>25</STREET_NUM> <STREET_ADDRESS>ELM ST</STREET_ADDRESS> <STREET_PREFIX/> <STREET_NAME>ELM</STREET_NAME> <STREET_TYPE>ST</STREET_TYPE> <STREET_SUFFIX/> </PROPERTY_ADDRESS> </PROPERTY> </RESPONSE> I have a class structure to match: [DataContract(Name="RESPONSE", Namespace="")] public class Response { [DataMember(Name="KEY")] public string Key { get; set; } [DataMember(Name = "PROPERTY")] public Property Property { get; set; } }

ShouldSerialize pattern and DataContractSerializer

北战南征 提交于 2019-11-28 11:39:31
Is there a way to get the ShouldSerialize* pattern working with DataContractSerializer ? Here is a small example: I have a simple class Person which looks like this: [DataContract] public class Person { [DataMember] public string FirstName { get; set; } public bool ShouldSerializeFirstName() { return !string.IsNullOrEmpty(FirstName); } [DataMember] public string LastName { get; set; } public bool ShouldSerializeLastName() { return !string.IsNullOrEmpty(LastName); } public Person(string firstName, string lastName) { FirstName = firstName; LastName = lastName; } public Person(string firstName) {

The deserializer has no knowlege of any type that maps to this contract

只愿长相守 提交于 2019-11-28 08:59:55
问题 I'm trying to serialize and deserialize a tree of Node objects. My abstract "Node" class as well as other abstract and concrete classes that derive from it are defined in my "Informa" project. In addition, I've created a static class in Informa for serialization / deserialization. First I'm deconstructing my tree into a flat list of type Dictionary(guid,Node) where guid is the unique id of Node. I am able to serialize all my Nodes with out a problem. But when I try to deserialize I get the

how to not return null when a Data member field is not set in the data contract

左心房为你撑大大i 提交于 2019-11-28 08:19:13
I'm having a strange problem with my WCF service that returns data in JSON format. I want to return information about a "Customer" based on the request sent by client. Client can request what fields of information about a customer they need and the service needs to send only that information about the customer. For Example: If client asks for a list of customers and says they want firstname, lastname, city of each customer then server should send a json response with each field name and corresponding value Something like... [ {"firstname":"john","lastname":"Goodman","city" :"NY"}, {"firstname"

Serializing object with no namespaces using DataContractSerializer

我们两清 提交于 2019-11-28 05:41:51
How do I remove XML namespaces from an object's XML representation serialized using DataContractSerializer? That object needs to be serialized to a very simple output XML. Latest & greatest - using .Net 4 beta 2 The object will never need to be deserialized. XML should not have any xmlns:... namespace refs Any subtypes of Exception and ISubObject need to be supported. It will be very difficult to change the original object. Object: [Serializable] class MyObj { string str; Exception ex; ISubObject subobj; } Need to serialize into: <xml> <str>...</str> <ex i:nil="true" /> <subobj i:type="Abc">