xml-serialization

.NET XML serialization gotchas?

I've run into a few gotchas when doing C# XML serialization that I thought I'd share: You can't serialize items that are read-only (like KeyValuePairs) You can't serialize a generic dictionary. Instead, try this wrapper class (from http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx): using System; using System.Collecti...

XML Serialization and empty collections.

I have a a property defined as: [XmlArray("delete", IsNullable = true)] [XmlArrayItem("contact", typeof(ContactEvent)), XmlArrayItem("sms", typeof(SmsEvent))] public List<Event> Delete { get; set; } If the List<> Delete has no items <delete /> is emitted. If the List<> Delete is set to null <delete xsi:nil="true" /> is emitted....

.NET XML Seralization

I'm working on a set of classes that will be used to serialize to XML. The XML is not controlled by me and is organized rather well. Unfortunately, there are several sets of nested nodes, the purpose of some of them is just to hold a collection of their children. Based on my current knowledge of XML Serialization, those nodes require ...

Ruby code for quick-and-dirty XML serialization?

Given a moderately complex XML structure (dozens of elements, hundreds of attributes) with no XSD and a desire to create an object model, what's an elegant way to avoid writing boilerplate from_xml() and to_xml() methods? For instance, given: <Foo bar="1"><Bat baz="blah"/></Foo> How do I avoid writing endless sequences of: class Fo...

How do I map XML to C# objects

I have an XML that I want to load to objects, manipulate those objects (set values, read values) and then save those XMLs back. It is important for me to have the XML in the structure (xsd) that I created. One way to do that is to write my own serializer, but is there a built in support for it or open source in C# that I can use? ...

PHP Associative arrays to and from XML

Is there an easy way to marshal a PHP associative array to and from XML? For example, if I have the following array: $items = array("1", "2", array( "item3.1" => "3.1", "item3.2" => "3.2" "isawesome" => true ) ); How would I turn it into something similar to the following XML in as few lines as possible...

Serializing DateTime to time without milliseconds and gmt

I have created a C# class file by using a XSD-file as an input. One of my properties look like this: private System.DateTime timeField; [System.Xml.Serialization.XmlElementAttribute(DataType="time")] public System.DateTime Time { get { return this.timeField; } set { this.timeField = value; } } ...

Can you force the serialization of an enum value into an integer?

Possible Duplicate: How do I serialize an enum value as an int? Hi, all! I'm wondering if there's a way to force the serialization of an enum value into its integer value, instead of its string representation. To put you into context: We're using, in a web application that heavily relies on web services, a single baseclass ...

How do I deserialize an XML file into a class with a read only property?

I've got a class that I'm using as a settings class that is serialized into an XML file that administrators can then edit to change settings in the application. (The settings are a little more complex than the App.config allows for.) I'm using the XmlSerializer class to deserialize the XML file, and I want it to be able to set the prop...

Generating an Xml Serialization assembly as part of my build.

This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer serializer = new XmlSerializer(typeof(MyClass)); //... } Here is the exception: A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Additional information: Could not load fil...

Why won't Visual Studio 2005 generate an Xml Serialization assembly?

Why isn't Visual Studio 2005 generating a serialization setting when I set the project setting "Generate Serialization Assembly" to "On"? ...

PHP Object as XML Document

What is the best way to take a given PHP object and serialize it as XML? I am looking at simple_xml and I have used it to parse XML into objects, but it isn't clear to me how it works the other way around. ...

Controlling the WCF XmlSerializer

I have some REST web services implemented in WCF. I wish to make these services return "Bad Request" when the xml contains invalid elements. The xml serialization is being handled by XmlSerializer. By default XmlSerializer ignores unknown elements. I know it is possible to hook XmlSerializer.UnknownElement and throw an exception from th...

[XmlRoot] attribute not included in the generated proxy class

Using .Net 3.0 and VS2005. The objects in question are consumed from a WCF service then serialized back into XML for a legacy API. So rather than serializing the TestObject, it was serializing .TestObject which was missing the [XmlRoot] attribute; however, all the [Xml*] attributes for the child elements were in the generated proxy cod...

Including arrary index in XML Serialization

I have a class that looks like this public class SomeClass { public SomeChildClass[] childArray; } which will output XML from the XMLSerializer like this: <SomeClass> <SomeChildClass> ... </SomeChildClass> <SomeChildClass> ... </SomeChildClass> </SomeClass> But I want the XML to look like this: <SomeCla...

Omitting XML processing instruction when serializing an object

I'm serializing an object in a C# VS2003 / .Net 1.1 application. I need it serialized without the processing instruction, however. The XmlSerializer class puts out something like this: <?xml version="1.0" encoding="utf-16" ?> <MyObject> <Property1>Data</Property1> <Property2>More Data</Property2> </MyObject> Is there any way ...

Slow SoapHttpClientProtocol constructor

I'm doing some experiments with Microsoft Dynamics CRM. You interact with it through web services and I have added a Web Reference my project. The web service interface is very rich, and the generated "Reference.cs" is some 90k loc. I'm using the web reference in a console application. I often change something, recompile and run. Compi...

XML Serialization, No Whitespace

I have the following serialization method: Private Function SerializeData(ByVal data As cData) As String If data IsNot Nothing Then Dim xml_stream As New MemoryStream() Dim sr As StreamReader Dim xs As New XmlSerializer(GetType(cData)) xml_stream = New MemoryStream() ...

Best Practice for Loading Object from Serialized XML in C#

Greetings, I have a particular object which can be constructed from a file, as such: public class ConfigObj { public ConfigObj(string loadPath) { //load object using .Net's supplied Serialization library //resulting in a ConfigObj object ConfigObj deserializedObj = VoodooLoadFunction(loadpath); ...

What is the best way to serialize xml into appropriate objects when versioned by namespaces?

My question is the following. I have xml that is versioned by a namespace. I want to serialize this received xml into the appropriate object, but the only way I know to do this means I have to process the xml two times. First to discover the namespace, and then in order to serialize to the object of the proper type based on the discov...