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.

4607 questions
368
votes
19 answers

XmlSerializer giving FileNotFoundException at constructor

An application I've been working with is failing when I try to serialize types. A statement like XmlSerializer lizer = new XmlSerializer(typeof(MyType)); produces: System.IO.FileNotFoundException occurred Message="Could not load file or assembly…
Irwin
  • 11,787
  • 10
  • 64
  • 92
332
votes
10 answers

Serialize an object to string

I have the following method to save an Object to a file: // Save an object out to the disk public static void SerializeObject(this T toSerialize, String filename) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); …
Vaccano
  • 70,257
  • 127
  • 405
  • 747
303
votes
17 answers

Serialize an object to XML

I have a C# class that I have inherited. I have successfully "built" the object. But I need to serialize the object to XML. Is there an easy way to do it? It looks like the class has been set up for serialization, but I'm not sure how to get the XML…
user462166
  • 3,775
  • 3
  • 16
  • 18
237
votes
12 answers

{" was not expected.} Deserializing Twitter XML

I'm pulling in the XML from Twitter via OAuth. I'm doing a request to http://twitter.com/account/verify_credentials.xml, which returns the following XML: 16434938 Lloyd Sparkes
lloydsparkes
  • 2,744
  • 2
  • 21
  • 20
195
votes
15 answers

Convert XML String to Object

I am receiving XML strings over a socket, and would like to convert these to C# objects. The messages are of the form: 1 stop I am new to .Net, and am not sure the best practice for performing this. I've…
Steve
  • 45,586
  • 30
  • 89
  • 135
174
votes
4 answers

Why XML-Serializable class need a parameterless constructor

I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object obj) { XmlSerializer serializer = new XmlSerializer(obj.GetType()); using (StringWriter writer = new StringWriter()) { …
Morgan Cheng
  • 66,562
  • 63
  • 166
  • 223
158
votes
7 answers

Is it possible to deserialize XML into List?

Given the following XML: 1 Joe 2 John And the following class: public class User { …
Daniel Schaffer
  • 52,912
  • 28
  • 108
  • 161
157
votes
5 answers

Proper way to implement IXmlSerializable?

Once a programmer decides to implement IXmlSerializable, what are the rules and best practices for implementing it? I've heard that GetSchema() should return null and ReadXml should move to the next element before returning. Is this true? And what…
Greg
  • 21,917
  • 11
  • 55
  • 77
148
votes
5 answers

Omitting all xsi and xsd namespaces when serializing an object in .NET?

The code looks like this: StringBuilder builder = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; using (XmlWriter xmlWriter = XmlWriter.Create(builder, settings)) { XmlSerializer s…
NetSide
  • 3,689
  • 8
  • 28
  • 41
141
votes
5 answers

XmlSerializer: remove unnecessary xsi and xsd namespaces

Is there a way to configure the XmlSerializer so that it doesn't write default namespaces in the root element? What I get is this: and I want to remove both xmlns…
Dave Van den Eynde
  • 16,092
  • 7
  • 56
  • 86
139
votes
7 answers

Xml serialization - Hide null values

When using a standard .NET Xml Serializer, is there any way I can hide all null values? The below is an example of the output of my class. I don't want to output the nullable integers if they are set to null. Current Xml output:
GuruMeditation
  • 1,642
  • 4
  • 14
  • 14
126
votes
5 answers

What is MyAssembly.XmlSerializers.dll generated for?

I am working on a project which generates an assembly. I just noticed that an additional assembly *.XmlSerializers.dll is being generated. Why this file is auto generated and what it is used for?
Hemant
  • 18,354
  • 22
  • 87
  • 123
121
votes
19 answers

.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…
Kalid
  • 20,388
  • 14
  • 40
  • 45
119
votes
4 answers

Serializing an object as UTF-8 XML in .NET

Proper object disposal removed for brevity but I'm shocked if this is the simplest way to encode an object as UTF-8 in memory. There has to be an easier way doesn't there? var serializer = new XmlSerializer(typeof(SomeSerializableObject)); var…
Garry Shutler
  • 31,001
  • 11
  • 77
  • 118
115
votes
6 answers

How to serialize an object to XML without getting xmlns="..."?

Is there a way for me to serialize an object in .NET without the XML Namespaces automatically serializing also? It seems that by default .NET believes the XSI and XSD namespaces should be included, but I don't want them there.
Wes P
  • 9,102
  • 14
  • 39
  • 48
1
2 3
99 100