Questions tagged [datacontractserializer]

.NET Framework class, Serializing and deserializing an instance of a type into an XML stream or document using a supplied data contract.

900 questions
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
103
votes
9 answers

WCF chokes on properties with no "set ". Any workaround?

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…
Andrey
  • 20,487
  • 26
  • 108
  • 176
100
votes
5 answers

DataContractSerializer doesn't call my constructor?

I just realized something crazy, which I assumed to be completely impossible : when deserializing an object, the DataContractSerializer doesn't call the constructor ! Take this class, for instance : [DataContract] public class Book { public…
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
68
votes
7 answers

"Type not expected", using DataContractSerializer - but it's just a simple class, no funny stuff?

I'm refactoring my XML-serialization, and figured I'd try the DataContractSerializer. Everything runs smoothly, until it needs to serialize this class: using System; using System.Runtime.Serialization; namespace…
Julian
  • 1,050
  • 1
  • 12
  • 23
68
votes
4 answers

Using DataContractSerializer to serialize, but can't deserialize back

I have the following 2 functions: public static string Serialize(object obj) { DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); MemoryStream memoryStream = new MemoryStream(); …
Dimskiy
  • 5,233
  • 13
  • 47
  • 66
68
votes
3 answers

XML Serialization and namespace prefixes

I'm looking for a way with C# which I can serialize a class into XML and add a namespace, but define the prefix which that namespace will use. Ultimately I'm trying to generate the following XML:
Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
59
votes
5 answers

Formatting of XML created by DataContractSerializer

Is there an easy way to get DataContractSerializer to spit out formatted XML rather then one long string? I don't want to change the tags or content in any way, just have it add line breaks and indentation to make the XML more readable?
Eric Anastas
  • 21,675
  • 38
  • 142
  • 236
51
votes
2 answers

XSLT Transform XML with Namespaces

I'm trying to transform some XML into HTML using XSLT. Problem: I can't get it to work. Can someone tell me what I'm doing wrong? XML:
Hungry Beast
  • 3,677
  • 7
  • 49
  • 75
43
votes
2 answers

Serializing object with no namespaces using DataContractSerializer

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…
42
votes
5 answers

DataContractSerializer - how can I output the xml to a string (as opposed to a file)

I had a quick question regarding the datacontractserializer. Maybe it's more of a stream question. I found a piece of code that writes the xml to a filestream. I basically don't want the file and just need the string output. public static string…
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
31
votes
1 answer

Ignore field order in DataContractSerializer

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…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
30
votes
2 answers

WCF Datacontract, some fields do not deserialize

Problem: I have a WCF service setup to be an endpoint for a call from an external system. The call is sending plain xml. I am testing the system by sending calls into the service from Fiddler using the RequestBuilder. The issue is that all of my…
Justin Hersh
  • 337
  • 1
  • 3
  • 7
28
votes
6 answers

WCF Service Reference - Getting "XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C" on Client Side

I have a smart client application communicating with its server via WCF. Data is created on the client and then sent over the service to be persisted. The server and client use the same domain classes via a shared dll and I'm using the handy "Add…
alt_tab
  • 352
  • 1
  • 3
  • 10
25
votes
1 answer

DataContractJsonSerializer to skip nodes with null values

I am using DataContractJsonSerializer to serialize my custom object to JSON. But i want to skip the data members whose values are null. If DataMember is null that node should not come in JSON string. How can I achieve this? Give me a simple code…
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
59 60