Questions tagged [datacontract]

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. A data contract precisely defines, for each parameter or return type, what data is serialized to be exchanged.

798 questions
237
votes
6 answers

Namespace for [DataContract]

I can't find the namespace to use for [DataContract] and [DataMember] elements. According to what I've found, it seems that adding the following should be enough, but in my case it is not. using System; using System.Runtime.Serialization; Here is a…
Otiel
  • 18,404
  • 16
  • 78
  • 126
202
votes
8 answers

When to use DataContract and DataMember attributes?

I am very confused about the DataContract attribute in WCF. As per my knowledge it is used for serializating user defined type like classes. I wrote one class which is exposed at client side like this. [DataContract] public class Contact { …
sam
  • 2,029
  • 2
  • 13
  • 3
63
votes
5 answers

WCF: Exposing readonly DataMember properties without set?

I have a server side class which I make available on the client side through a [DataContract]. This class has a readonly field which I'd like to make available through a property. However, I'm unable to do so because it doesn't seem that I'm allowed…
stiank81
  • 25,418
  • 43
  • 131
  • 202
58
votes
10 answers

Serialize enum to string

I have an enum: public enum Action { Remove=1, Add=2 } And a class: [DataContract] public class Container { [DataMember] public Action Action {get; set;} } When serialize instance of Container to json I get: {Action:1} (in case…
Naor
  • 23,465
  • 48
  • 152
  • 268
51
votes
2 answers

IsReference property in data contract

What is the purpose of IsReference property in DataContract? How does the request and response vary with this property applied?
web dunia
  • 9,381
  • 18
  • 52
  • 64
45
votes
6 answers

Configure JSON.NET to ignore DataContract/DataMember attributes

We are running into a situation on an MVC3 project with both the Microsoft JSON serializers and JSON.NET. Everybody knows DateTime's are basically broken in Microsoft's serializers, so we switched to JSON.NET to avoid this issue. That works great,…
Nick
  • 9,113
  • 4
  • 25
  • 29
44
votes
2 answers

DataContract XML serialization and XML attributes

Is it possible to deserialize this XML into an object marked with the DataContract attribute? 1000 As you may see there is "units" attribute. I don't believe that's supported. Or am I wrong?
Schultz9999
  • 8,717
  • 8
  • 48
  • 87
43
votes
3 answers

Portable class library: recommended replacement for [Serializable]

I am porting a .NET Framework C# class library to a Portable Class Library. One recurring problem is how to deal with classes decorated with the [Serializable] attribute, since this attribute is not part of the Portable Class Library subset.…
32
votes
1 answer

Why is using [DataMember(EmitDefaultValue = false)] not recommended?

In WCF you can define a contract using the [DataContract] and [DataMember] attributes, like this: [DataContract] public class Sample { [DataMember(EmitDefaultValue = false, IsRequired = false)] public string Test { get; set; } } This…
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
28
votes
2 answers

Can I use DataContract and Serializable together?

I am working on WCF service. My all class are already serialize using [Serializable] attribute but due to "k__BackingField" Property Naming problem I used DataContract and DataMember attribute. so Can i use both attribute together like…
Rajesh Kumar
  • 2,443
  • 3
  • 28
  • 43
27
votes
4 answers

Generate DataContract from XSD

I want to be able to generate a DataContract from a XSD file, preferably using the xsd.exe tool. What is the easiest way for it to auto generate the [DataContract] and [DataMember] on each of my items? Or is there a better approach? I am trying to…
Daveo
  • 19,018
  • 10
  • 48
  • 71
25
votes
1 answer

DataContract serialization exception (data contract name is not expected)

I have the following code: [DataContract] class TestContract { private String _Name; private Int32 _Age; [DataMember( Name = "Name" )] public String Name { get { return _Name; } set { _Name = value; } } …
Aaaaaaaa
  • 2,015
  • 3
  • 22
  • 40
23
votes
3 answers

DataContract, default DataMember value

Is there a way to choose default values of attributes that are not in the xml file during deserialization? If the mAge attribute is not present in the xml file, I want to use a default value of 18. Is it possible ? [DataContract] public class…
BuzBuza
  • 607
  • 2
  • 7
  • 19
22
votes
2 answers

Using WCF with abstract classes

How do I define DataContract for abstract classes in WCF? I have a class "Person" which I communicate successfully using WCF. Now I add a new class "Foo" referenced from Person. All still good. But when I make Foo abstract and define a sub class…
stiank81
  • 25,418
  • 43
  • 131
  • 202
22
votes
6 answers

Passing an instance of anonymous type over WCF

I have a WCF service method that expects an object and then retrieves its properties using reflection. On the client side I create an anonymous type object var obj = new {FirstName="John", LastName="Doe"} and pass it to the method. I'm getting an…
Andrey
  • 20,487
  • 26
  • 108
  • 176
1
2 3
53 54