datacontractserializer

How to serialize class type but not the namespace to a Json string using DataContractJsonSerializer

送分小仙女□ 提交于 2019-12-03 12:10:23
问题 I'm trying to serialize a class hierarchy to a Json string using DataContractJsonSerializer , in a WCF service. the default behaviour for serializing a derived class is to add the following key value pair to the object: "__type":"ClassName:#Namespace" My problem is that namespaces are long and they bloat the Json string. I would like to somehow intervene with the serialization and output this instead: "__type":"ClassName" and on deserialization intervene again to point to the correct

Linq to Xml VS XmlSerializer VS DataContractSerializer

眉间皱痕 提交于 2019-12-03 06:53:55
问题 In my web method , I get an object of some third party C# entity class. The entity class is nothing but the DataContract . This entity class is quite complex and has properties of various types, some properties are collections too. Of course, those linked types are also DataContracts. I want to serialize that DataContract entity into XML as part of business logic of my web service. I cannot use DataContractSerializer directly (on the object I receive in the web method) simply because the XML

Formatting of XML created by DataContractSerializer

半世苍凉 提交于 2019-12-03 05:28:00
问题 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? <tagA> <tagB>This is</tagB> <tagC>Much</tagC> <tagD> <tagE>easier to read</tagE> </tagD> </tagA> <tagA><tagB>This is</tagB><tagC>Much</tagC><tagD><tagE>harder to read</tagE></tagD></tagA> 回答1: As bendewey says, XmlWriterSettings is what you need - e.g.

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

安稳与你 提交于 2019-12-03 04:06:44
问题 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 DataContractSerializeObject<T>(T objectToSerialize) { var fs = new FileStream("test.xml", FileMode.OpenOrCreate); var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(fs, objectToSerialize); fs.Close(); return fs.ToString();

How to serialize class type but not the namespace to a Json string using DataContractJsonSerializer

泄露秘密 提交于 2019-12-03 02:41:48
I'm trying to serialize a class hierarchy to a Json string using DataContractJsonSerializer , in a WCF service. the default behaviour for serializing a derived class is to add the following key value pair to the object: "__type":"ClassName:#Namespace" My problem is that namespaces are long and they bloat the Json string. I would like to somehow intervene with the serialization and output this instead: "__type":"ClassName" and on deserialization intervene again to point to the correct namespace (which i know in runtime). Is there any way to do such a thing? This page describes the circumstances

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

久未见 提交于 2019-12-02 22:08:37
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 Service Reference" functionality in Visual Studio that wraps SvcUtil.exe and generates the client and proxy classes. I get the following error when trying to call the service: System.Xml.XmlException occurred Message=Name cannot begin with the '<' character, hexadecimal value 0x3C. Source=System.Xml LineNumber=0 LinePosition=1 StackTrace: at System

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

蓝咒 提交于 2019-12-02 17:26:26
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 DataContractSerializeObject<T>(T objectToSerialize) { var fs = new FileStream("test.xml", FileMode.OpenOrCreate); var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(fs, objectToSerialize); fs.Close(); return fs.ToString(); } fs.ToString() is obviously not what I'm looking for. What stream or writer etc, can I use just to

Getting bad generated code from “Update Service Reference”

非 Y 不嫁゛ 提交于 2019-12-02 14:32:56
问题 In VB.NET (using Visual Studio 2008) my WCF service has an interface something like: <ServiceContract()> _ Public Interface IThingService <OperationContract()> _ Function GetThingByNumber(ByVal thingNumber As MyKeyClass) As Thing <OperationContract()> _ Function GetThing(ByVal thingId As Guid) As Thing ' ... End Interface I recently changed two projects with similar code to use a basicHttpBinding rather than a wsHttpBinding. Everything compiles well on the service side. Now, in the a client

How to deserialize object from OData Atom feed?

你离开我真会死。 提交于 2019-12-02 10:48:24
问题 I am trying to parse response from an OData REST service. When response is in JSON format, it is easy to use ReadAsJsonDataContract method from WCF REST starter kit. However things seem to be more complicated in case the response is an Atom feed. This is an example: <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> <entry xml:base="http://localhost:64172/BookshopService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado

Events not working with after Deserialization

心不动则不痛 提交于 2019-12-02 04:22:01
PROBLEM: I have a Child class which uses DataContractSerialization and raises a Changed event when its Name property is set. <DataContract()> Public Class Child Public Event Changed() <DataMember()> Private _Name As String Public Sub New(ByVal NewName As String) _Name = NewName End Sub Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value RaiseEvent Changed() End Set End Property End Class It is contained within a Parent class which also uses DataContractSerialization and handles the Changed event of the Child. <DataContract()> Public Class Parent