Questions tagged [xml-serialization]

This tag refers to serialization technologies which use XML as a data format.

This tag refers to serialization technologies which use XML as a data format. In the context of .NET, it refers to the XmlSerializer class, less so to the DataContractSerializer class, still less to the SoapFormatter class.

4687 questions
69
votes
9 answers

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…
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
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
67
votes
4 answers

Suppress Null Value Types from Being Emitted by XmlSerializer

Please consider the following Amount value type property which is marked as a nullable XmlElement: [XmlElement(IsNullable=true)] public double? Amount { get ; set ; } When a nullable value type is set to null, the C# XmlSerializer result looks…
Ben Griswold
  • 17,793
  • 14
  • 58
  • 60
66
votes
5 answers

What is the best way to parse (big) XML in C# Code?

I'm writing a GIS client tool in C# to retrieve "features" in a GML-based XML schema (sample below) from a server. Extracts are limited to 100,000 features. I guestimate that the largest extract.xml might get up around 150 megabytes, so obviously…
corlettk
  • 13,288
  • 7
  • 38
  • 52
65
votes
7 answers

Why isn't my public property serialized by the XmlSerializer?

This is one i struggled with for ages so thought I'd document somewhere. (Apologies for asking and answering a question.) (C# .net 2.0) I had a class that was being serialized by XmlSerializer, I added a new public property however it wasn't being…
Rory
  • 40,559
  • 52
  • 175
  • 261
65
votes
5 answers

Serialize Python dictionary to XML

There is simple JSON serialization module with name "simplejson" which easily serializes Python objects to JSON. I'm looking for similar module which can serialize to XML.
tefozi
  • 5,390
  • 5
  • 38
  • 52
64
votes
1 answer

Deserializing into a List without a container element in XML

In all the examples I've seen of using XmlSerializer any time a list or array happens you have some sort of container element like this: One Two Three
Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
62
votes
7 answers

Obsolete attribute causes property to be ignored by XmlSerialization

I'm refactoring some objects that are serialized to XML but need to keep a few properties for backwards compatibility, I've got a method that converts the old object into the new one for me and nulls the obsolete property. I want to use the Obsolete…
Rob Stevenson-Leggett
  • 35,279
  • 21
  • 87
  • 141
59
votes
4 answers

Reading from memory stream to string

I am trying to write an object to an Xml string and take that string and save it to a DB. But first I need to get the string... private static readonly Encoding LocalEncoding = Encoding.UTF8; public static string SaveToString (T…
tigerswithguitars
  • 2,497
  • 1
  • 31
  • 53
56
votes
3 answers

Which characters are Invalid (unless encoded) in an XML attribute?

I can't believe I can't find this information easily accessible, so: 1) Which characters cannot be incorporated in an XML attribute without entity-encoding them? Obviously, you need to encode quotes. What about < and >? What else? 2) Where exactly…
Euro Micelli
  • 33,285
  • 8
  • 51
  • 70
54
votes
5 answers

Can I make XmlSerializer ignore the namespace on deserialization?

Can I make XmlSerializer ignore the namespace (xmlns attribute) on deserialization so that it doesn't matter if the attribute is added or not or even if the attribute is bogus? I know that the source will always be trusted so I don't care about the…
NotDan
  • 31,709
  • 36
  • 116
  • 156
52
votes
13 answers

Which is the best alternative for Java Serialization?

I'm currently working on a project which needs to persist any kind of object (of which implementation we don't have any control) so these objects could be recovered afterwards. We can't implement an ORM because we can't restrict the users of our…
Alotor
  • 7,407
  • 12
  • 38
  • 36
51
votes
10 answers

How to exclude null properties when using XmlSerializer

I'm serializing a class like this public MyClass { public int? a { get; set; } public int? b { get; set; } public int? c { get; set; } } All of the types are nullable because I want minimal data stored when serializing an object of this…
Allen Rice
  • 19,068
  • 14
  • 83
  • 115