xmlserializer

Why is XmlSerializer so hard to use?

I imagine to use XML serialization like this: class Foo { public Foo (string name) { Name1 = name; Name2 = name; } [XmlInclude] public string Name1 { get; private set; } [XmlInclude] private string Name2; } StreamWriter wr = new StreamWriter("path.xml"); new XmlSerializer<Foo>().Serialize (wr, ...

XmlSerializer and OnSerializing/OnSerialized alternatives

I have various complex objects that often have collections of other complex objects. Sometimes I only want to load the collections when they're needed so I need a way to keep track of whether a collection has been loaded (null/empty doesn't necessarily mean it hasn't been loaded). To do this, these complex objects inherit from a class ...

XmlSerializer equivalent of IExtensibleDataObject

With DataContracts you can derive from IExtensibleDataObject to allow round-tripping to work without losing any unknown additional data from your XML file. I can't use DataContract because I need to control the formatting of the output XML. But I also need to be able to read a future version of the XML file in the old version of the app...

XMlSerialization is not serializing a Datetime

When I serialize an object which has a DateTime in it this is returning empty in the XML string. Please see below for my XSD, serializable class generated from the XSD, and serialization helper class which handles the serialization of the XSD. XSD: <?xml version="1.0" encoding="utf-8"?> <xs:schema id="test" xmlns="" xmlns:xs="ht...

XmlSerializer (c#) reports There was an error reflecting type (type = List<myclass> )

Hi there, Can anyone help? I have a method for converting an object to a dataset. In this case the object is a collection (using LIST) of a a collection class Here is the code but it errors with the following error on the line XmlSerializer formatter= ... This is the error There was an error reflecting type 'System.Collections.Gener...

XmlSerializer.Deserialize method appends timezone to a time and datetime field

Have a small script in Microsoft.NET 2.0 that deserializes a XML back to a typed object, connects dyanimcally to a web service using ServiceDescription and binds the deserialized typed object to the WebMethod inbound. The XML prior to serialization looks like below <completion_time>12:19:38</completion_time> on the wire when communic...

Changing what a property is serialized as

I think i already know the answer to this, but i cannot find anything that states it definitively, hence my question - i want to make sure i am not missing a trick. Using the DataContractSerializer or the XmlSerializer, is there any way to change what a pulic property is serialized as? I have a property that is an Enum, and i would lik...

XmlSerializer constructor error with class derived from a base class

Hi @all the following code specifies a type "MyBase64Binary" which is derived from a base class "TestBase" using System; using System.Xml.Serialization; using System.Collections; using System.Xml.Schema; using System.ComponentModel; namespace Test { public class TestBase { public TestBase() { } } ...

how to: re-assemble machine generated classes from xsd files to their original nested state.

Hi everyone, I'm working in Visual Studio 2008 using c#. Let's say I have 2 xsd files e.g "Envelope.xsd" and "Body.xsd" I create 2 sets of classes by running xsd.exe, creating something like "Envelope.cs" and "Body.cs", so far so good. I can't figure out how to link the two classes to serialize (using XmlSerializer) into the proper n...

WCF Custom WSDL XmlSerializerOperationBehavior

Hi, I have code that builds a custom WCF wsdl on the fly. In one particular scenario, the WSDL exported should use the XmlSerializerOperationBehavior as the IWsdlExportExtension as opposed to the default DataContractSerializerOperationBehavior IWsdlExportExtension. However, every time I try this from the WSDL generation code, I get a ...

How does DataContractSerializer write to private fields?

I understand how XMLSerializer could work by using reflection to figure out what public read/write fields or properties it should be using to serialize or de-serialize XML. Yet XMLSerializer requires that the fields be public and read/write. However, DataContractSerializer is able to read or write to or from completely private fields i...

Interchange xsd and xsi in the output of XmlSerializer

XmlSerializer serializer = new XmlSerializer(typeof(IxComment)); System.IO.StringWriter aStream = new System.IO.StringWriter(); serializer.Serialize(aStream,Comments); commentsString = aStream.ToString(); Here the commentsString has the the following element in it <IxComment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:...

XmlSerializer Deserialize failures

I have wsdl from third party server. Ran svcutil and ended up wih a set of XmlNode AMethod(object Request); methods. There is a separate 100 page pdf describing response/request objects for each method My thought was wrap web methods and use XmlSerializer to return strongly typed objects. Returned xml looks like this (i removed ...

XmlSerializer with custom tranform using XSLT

Is there a way to deserialize a XML stream using XmlSerializer by applying a custom tranform defined in a XSLT? ...

XmlSerializer with multiple classes

Hi, What's the best way to serialize multiple classes with XmlSerializer in one file? Ideally, what I want is to have one root node and the XmlSerializer serializes one child node to this root node, one per class. Otherwise, my other idea is to just make a wrapper that contains these classes and serialize that. ...

XmlSerializer Mixed Content Deserialization

How do I setup my class to deserialize the following mixed content xml? <employee> <name>John Doe</name> <remark>He is a <match>tall</match> and handsome man</remark> </employee> ...

Using generics with XmlSerializer

Hi, When using XML serialization in C#, I use code like this: public MyObject LoadData() { XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyObject)); using (TextReader reader = new StreamReader(settingsFileName)) { return (MyObject)xmlSerializer.Deserialize(reader); } } (and similar code for deserializ...

Is there a way to make a serialized member to serialize as an attribute?

Is there a way to make a serialized member to serialize as an attribute: <Serializable> Public Class Person Public Property Name As String End Class I want than when this class is xml-serialized, it should produce: <Person Name="John Doe" /> And what I mean is that instead of the Name property should be serialized as an elemen...

Automatically minifying attribute/element names when using XmlSerializer

When serializing a C# class using XmlSerializer, the attributes/elements representing the properties of the class will have the same names as they do in the source code. I know you can override this by doing like so: [XmlAttribute("num")] public int NumberOfThingsThatAbcXyz { get; set; } I'd like the generated XML for my classes to b...

Unknown attribute xsi:type in XmlSerializer

I am learning XML Serialization and meet an issue, I have two claess [System.Xml.Serialization.XmlInclude(typeof(SubClass))] public class BaseClass { } public class SubClass : BaseClass { } I am trying to serialize a SubClass object into XML file, I use blow code XmlSerializer xs = new XmlSerializer(typeof(Base)); xs.Serialize(fs, ...