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
116
votes
5 answers

Why are properties without a setter not serialized

I have a serializable class and one of the properties in my class generates a Guid in the getter. The property implements no setter and is ignores during serialization. Why is that and do I always have to implement a setter in order for my property…
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
114
votes
4 answers

How can I make the xmlserializer only serialize plain xml?

I need to get plain xml, without the at the beginning and xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" in first element from XmlSerializer. How can I do…
Grzenio
  • 35,875
  • 47
  • 158
  • 240
110
votes
15 answers

Checking if an object is a number

I'd like to check if an object is a number so that .ToString() would result in a string containing digits and +, -, . Is it possible by simple type checking in .NET? Like: if (p is Number) Or should I convert to string, then try parsing to…
Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122
109
votes
7 answers

Using StringWriter for XML Serialization

I'm currently searching for an easy way to serialize objects (in C# 3). I googled some examples and came up with something like: MemoryStream memoryStream = new MemoryStream ( ); XmlSerializer xs = new XmlSerializer ( typeof ( MyObject)…
StampedeXV
  • 2,715
  • 2
  • 24
  • 40
99
votes
9 answers

How do you serialize a string as CDATA using XmlSerializer?

Is it possible via an attribute of some sort to serialize a string as CDATA using the .Net XmlSerializer?
jamesaharvey
  • 14,023
  • 15
  • 52
  • 63
90
votes
6 answers

Force XmlSerializer to serialize DateTime as 'YYYY-MM-DD hh:mm:ss'

I have a XSD schema for some RESTful service. When used in conjunction with xsd.exe tool to generate C# code, XSD's xs:date generates the following code: [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,…
wpfwannabe
  • 14,587
  • 16
  • 78
  • 129
86
votes
7 answers

XML Serialization and Inherited Types

Following on from my previous question I have been working on getting my object model to serialize to XML. But I have now run into a problem (quelle surprise!). The problem I have is that I have a collection, which is of a abstract base class type,…
Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
84
votes
7 answers

How do I serialize an enum value as an int?

I want to serialize my enum-value as an int, but i only get the name. Here is my (sample) class and enum: public class Request { public RequestType request; } public enum RequestType { Booking = 1, Confirmation = 2, PreBooking = 4, …
Espo
  • 41,399
  • 21
  • 132
  • 159
82
votes
5 answers

How can I rename class-names via Xml attributes?

Suppose I have an XML-serializable class called Song: [Serializable] class Song { public string Artist; public string SongTitle; } In order to save space (and also semi-obfuscate the XML file), I decide to rename the xml…
invarbrass
  • 2,023
  • 4
  • 20
  • 23
82
votes
2 answers

Serialize Property as Xml Attribute in Element

I have the following class: [Serializable] public class SomeModel { [XmlElement("SomeStringElementName")] public string SomeString { get; set; } [XmlElement("SomeInfoElementName")] public int SomeInfo { get; set; } } Which (when…
IUnknown
  • 2,596
  • 4
  • 35
  • 52
78
votes
6 answers

JAXB: How to ignore namespace during unmarshalling XML document?

My schema specifies a namespace, but the documents don't. What's the simplest way to ignore namespace during JAXB unmarshalling (XML -> object)? In other words, I have instead of,
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
78
votes
10 answers

XML Serialize generic list of serializable objects

Can I serialize a generic list of serializable objects without having to specify their type. Something like the intention behind the broken code below: List serializableList = new List(); XmlSerializer xmlSerializer =…
Simon D
  • 4,150
  • 5
  • 39
  • 47
73
votes
8 answers

Why isn't there an XML-serializable dictionary in .NET?

I need an XML-serializable dictionary. Actually, I now have two quite different programs that need one. I was rather surprised to see that .NET doesn't have one. Can someone enlighten me, given how dependent various .NET features are on XML…
serialhobbyist
  • 4,768
  • 5
  • 43
  • 65
72
votes
5 answers

Deciding on when to use XmlDocument vs XmlReader

I'm optimizing a custom object -> XML serialization utility, and it's all done and working and that's not the issue. It worked by loading a file into an XmlDocument object, then recursively going through all the child nodes. I figured that perhaps…
PhilChuang
  • 2,556
  • 1
  • 23
  • 29
69
votes
2 answers

Ignore a property during xml serialization but not during deserialization

In C#, how can I make XmlSerializer ignore a property during serialization but not during deserialization? (Or how do I do the same with Json.net?) To prevent a property from being serialized, you can add the XmlIgnore attribute: [XmlIgnore] public…
Manoj
  • 1,005
  • 1
  • 9
  • 11