datacontractserializer

DataContractSerializer: Handling objects whose type you don't know and don't have access to.

I have a class (call it Container) which is serialized and deserialized using the DataContractSerializer. The class contains a collection of other classes, all of which inherit the same base class (call it ContentsBase), but which have distinct derived classes. The application may be passed a serialized Container object which contains Co...

Why doesn't the XmlSerializer need the type to be marked [Serializable]?

In C#, if I want to serialize an instance with XmlSerializer, the object's type doesn't have to be marked with [Serializable] attribute. However, for other serialization approaches, such as DataContractSerializer, needs the class be marked as [Serializable] or [DataContract]. Is there any standard or pattern about serialization requirem...

Can you control the depth of DataContractSerializer deserialization?

I have a pretty sizeable object graph that I have serialized to a file via the DataContractSerializer. I now want to present a list of these files to the user to choose from. In this list, I want to show some of the details about the file, which are properties of the root object. I don't want to load the whole graph into memory, since...

How can I customize my WCF service to use DataContractSurrogate only in one EndPoint?

Hi, In my wcf service, I am interested in using DataContractSurrogate while exposing the ServiceContract in a particural EndPoint(e.g. WebHttpBinding). How can I do it ? Implementing IEndpointBehavior? In addition to that,do I need to implement IWsdlExportExtension? Thanks, Adil ...

DataContractSerializer within LINQ to SQL expression?

Hi, I'm new to LINQ expressions and trying to get the following to work: public IQueryable<Models.Powerups.Powerup> GetPowerups(Guid userid) { return from p in dc.Powerups where p.UserId == userid select (Models.Powerups.Powerup) new DataContractSerializer(Type.GetType(p.Type, true)).ReadObject(new XmlTextReader(new StringRea...

Can I force svcutil.exe to generate data contracts for a WCF service?

I would like to force svcutil to generate all data contracts in an assembly that is used by WCF, regardless of whether or not a type is referenced by a given operation contract. [DataContract] public class Foo { } [DataContract] public class Bar : Foo { } [ServiceContract] public interface IService { [OperationContract] void G...

DataContractSerializer missing from the .NET Compact Framework

It appears that the DataContractSerializer isn't available in the .NET Compact Framework. I found this quite surprising, as I consider DataContractSerializer to be the Holy Grail of serialization, and one of the most widely useful classes introduced in .NET 3. Is there a way to get the same functionality under the Compact Framework, tha...

XmlTextAttribute equivalent in DataContracts

Hi Is there an equivalent to XmlTextAttribute when using DataContractSerializer? I want to treate a property as the 'default property when it is serialized to XML. For example: [Serializable] [DataContract] public class Item { [DataMember] public String Value { get; set; } } A object with .Value set t...

DataContractSerializer skip OpenAccess Version's value

Hi, I'm using OpenAccess disconnected model. When I try to deserialize an object with DataConractSerializer, the Version property of this object is 0 - but only in the xml. If I debug the code and watch the value - it's 1 (or 2,3...) If I say before the serialization "int temp = object.Version" the seriazlier can save the value. There...

DataContractSerializer documentation

I'm looking for a good in depth documentation/how-to for DataContractSerializer. Google turns up a number of related articles but nothing jumped out at me. (I'm not looking for the MSDN link) A little clarification: I have never use serialization in .NET but have a working app that does that I need to update/modify. I have a fairly good...

XML and DataContractSerializer

I have classes like this [DataContract(Namespace = "")] public class Foo { [DataMember(Order = 0)] Bar bar; } [DataContract(Namespace = "")] public class Bar { Baz baz; [DataMember(Order = 0)] string TheBaz { get { baz.ToString(); } set { SomeOtherCode(value); } } } I want this to generate...

getting of the parent object using DataContractSerializer during deserialization

More or less the same as this question but for DataContractSerializer rather than Serializable. I explicitly do not want to be serializing parent references. Also I would really rather the object's parent be bound before the children are constructed. BTW: I'm using XML if that makes any difference ...

How to set the StreamingContext for DataContractSerializer?

I have some code something like this: [DataContract] class Foo { [OnSerializing] private void BeforeSerialize(StreamingContext ctx) { ((MtType)ctx.Context).DoStuff() } ... } var reader = new XmlTextReader(filename); var serializer = new DataContractSerializer(typeof(Type)); Type type = (Type)serializer.Read...

Can I configure the DataContractSerializer to not create optional (i.e. Nullable<> and List<>) elements in output XML?

I am using the new .NET 3.0 DataContractSerializer. I have both Nullable<> and List<> objects I am going to serialize. Example: [DataContract(Namespace = "")] class Test { public static void Go() { Test test = new Test(); var dcs = new DataContractSerializer(typeof(Test)); dcs.WriteObject(new StreamWri...

When using ISerializable with DataContractSerializer, how do I stop the serializer from outputting type information?

To get more control over serialization, I have converted a class from [DataContract] to [Serializable], implementing both GetObjectData and the special deserializing constructor. When I do this, the XML emitted now has type information applied to all elements. I don't want this superfluous information, and I'm wondering how to inform t...

WebInvoke/DataContractJsonSerializer, '1,6276' cannot be parsed as 'double'.

Hi, I am using WCF WebInvokeAttribute for declarative JSON requests (DataContractJsonSerializer), with DataContractAttribute/DataMemberAttribute based serialization. I'm using a service that supports returning JSON containing data based on different cultures. By default, this service uses en-US culture settings, which means the decimal...

Can you get conditional control over serialization with DataContractSerializer?

I'm converting some of my classes to use DataContractSerialization so that I can include Linq Entities in the output. A sort of theoretical question popped into my head while I was in the process, and while I'm betting the answer is "No" I figured I'd pose the question anyway. Is there a way to conditionally serialize an object? For i...

XSL Stylesheet won't parse XML generated by DataContractSerializer

I had some classes I was serializing out using XMLSerializer, then transforming using XSLT into various formatted emails or web service calls. All was well. We started using Linq in some parts of the data layer, and I needed to serialize out some of the Linq objects to be consumed by the XSL Stylesheets. I decided it was probably bes...

Exposing Objects (DataContracts) that may not be directly used by a WCF service?

I have some object classes that use inheritance. It seems that I can only get access to the objects that are directly used by a service. Let me show you what I am trying to accomplish: [DataContract] public class Object1 { [DataMember] int Id {get; set;} } [DataContract] public class object2: Object1 { [DataMember] string N...

Serialization / Derialization of a tree structure

I'm trying to figure out the best way to save (serialize) and later open (deserialize) a tree structure. My structure is made up of various object types with different properties, but each inherits from a base abstract "Node" class. Each node has unique ID (GUID), and has an AddSuperNode(Node nd) method that will set the parent of a nod...