Doesn't deserialize one instance of an object - c#

I have a rather complex data model that gets serialized to and from XML. We all know that when C#'s XmlSerializer serializes the same instance of a object multiple times that objects data get duplicated. I am having an issue of one instance of an object not deserializing. For example, let's say we have three objects: A, B, C. Lets say that A contains two instances of B, and B contains an list of C.
A
->B
->List<C> -- This one deserializes
->B
->List<C> -- This one does not
When I deserialize object A, the first instance of B deserializes correctly. However, when I inspect the second instance of B the List<C> is empty. I have ran a difference compare on the sections XML and they are the same.
Does anyone know why one list would deserialize and not the other?
UPDATE
This is the best that I can pair down the problem. The original XML is about 110,000 lines long.
Classes:
[Serializable]
public class A
{
public B instanceOne {get; set;}
public B instanceTwo {get; set;}
}
[Serializable]
public class B : INotifyPropertyChanged
{
private C _c;
public ObservableCollection<C> C
{
get => _c;
set
{
if(_c == value)
return;
_c = value;
RaisePropertyChanged(nameof(C));
}
}
//More Code
}
[Serializable, XmlRoot(ElementName = "MyC", Namespace = "MyNS")]
public class C
{
public int value {get;set;}
}
XML output:
<?xml version="1.0"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<InstanceOne>
<C>
<Value xmlns="MyNS">10</Value>
</C>
</InstanceOne>
<InstanceTwo>
<C>
<Value xmlns="MyNS">10</Value>
</C>
</InstanceTwo>
</A>
C# Deserialization Code
XmlSerializer xml = new XmlSerializer(typeof(A));
using (FileStream fs = File.OpenRead(#"C:\a.xml"))
{
var t = xml.Deserialize(fs);
}

Lists should have proper XmlElement attributes along with names.
And empty lists (without elements are skipped). It works on multiple levels.
I reproduced this :
[Serializable]
public class A
{
public A()
{
this.instanceOne = new B();
this.instanceTwo = new B();
}
public B instanceOne { get; set; }
public B instanceTwo { get; set; }
}
[Serializable]
public class B
{
public B()
{
this._c = new C();
this._c.value = 10;
}
private C _c;
}
[Serializable, XmlRoot(ElementName = "MyC", Namespace = "MyNS")]
public class C
{
public int value { get; set; }
}
class Program
{
static void Main(string[] args)
{
var serializer = new XmlSerializer(typeof(A));
var a = new A();
try
{
var fs = new System.IO.FileStream("test.xml", System.IO.FileMode.Create);
serializer.Serialize(fs, a);
fs.Close();
A d = (A)serializer.Deserialize(new System.IO.FileStream("test.xml", System.IO.FileMode.Open));
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}

I was not able to solve the issue at hand by calling the XmlSerializer. I ended up parsing the file then going back through the XML with the XmlReader to fill in the specific missing gaps.

Related

C# deserialize XML to a custom class

I have one single configuration XML file which can be deserialised into different instances, each of which takes only a portion of this XML.
class A
{
public string A { get; set; }
}
class B
{
public string B { get; set; }
}
public static T Xml2Cls<T>(string filename)
{
XDocument doc = XDocument.Load(filename);
var root = new XMLRootAttribte(doc.Root.Name.LocalName);
var serializer = new XMLSerializer(typeof(T), root);
using (var reader = doc.Root.CreateReader())
{
return (T)serializer .Deserialize(reader);
}
}
// if I keep calling the following for say 1000 times
// all these instances don't seem to get garbage collected
var a = Xml2Cls<A>("a.xml");
var b = Xml2Cls<B>("a.xml");
<root>
<a>...</a>
<b>...</b>
</root>
Any ideas why those instantiated objects are not garbage collocted? I tried moving the return out of the using block but it doesn't make a difference.

Serialization of object of one class and setting properties of other class

I am trying to understand the xml serialization/Deserialization behavior in C#. I am working with following example code:
[Serializable]
public class Class1 {
Class2 c2 = new Class2( );
public List<double> Arr2 {
get { return c2.Arr1 ;}
set { c2.Arr1 = value ;}
}
[XmlIgnore]
public Class2 C2 {
get { return c2; }
set { c2 = value; }
}
public Class1( ) {
}
}
public class Class2 {
private List<double> arr1;
public List<double> Arr1 {
get { return arr1; }
set { arr1 = value; }
}
public Class2( ) {
arr1 = (new double[ 5 ]).ToList();
}
}
Every time when I deserialize the xml file for class1, I get zeros in Arr2 instead of values from xml file. I am using following lines for deserialization:
public Class1 c1 = new Class1 () ;
XElement rootnode = XElement.Load( path );
c1 = rootnode.XmlDeserialize<Class1>( "Class1" );
Xml file:
<Class1>
<Arr2>
<double>1</double>
<double>2</double>
<double>3</double>
<double>4</double>
<double>5</double>
</Arr2>
</Class1>
You need to add the [Serializable] attribute to class2.
OK, from the comment, I can see I was wrong.
This worked for me:
XElement rootnode = XElement.Load( path );
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Class1));
var c1 = (Class1)serializer.Deserialize(rootnode.CreateReader());

Deserialize nested XML element into class in C#

I have the following XML structure (edited for brevity) which I have no control over.
<GetVehicles>
<ApplicationArea>
<Sender>
<Blah></Blah>
</Sender>
</ApplicationArea>
<DataArea>
<Error>
<Blah></Blah>
</Error>
<Vehicles>
<Vehicle>
<Colour>Blue</Colour>
<NumOfDoors>3</NumOfDoors>
<BodyStyle>Hatchback</BodyStyle>
<Vehicle>
</Vehicles>
</DataArea>
</GetVehicles>
I have the following Class:
[XmlRoot("GetVehicles"), XmlType("Vehicle")]
public class Vehicle
{
public string Colour { get; set; }
public string NumOfDoors { get; set; }
public string BodyStyle { get; set; }
}
I want to be able to deserialize the XML into a single instance of this Vehicle class. 99% of the time, the XML should only return a single 'Vehicle' element. I'm not yet dealing with it yet if it contains multiple 'Vehicle' elements inside the 'Vehicles' element.
Unfortunately, the XML data isn't currently being mapped to my class properties; they are being left blank upon calling my Deserialize method.
For completeness, here is my Deserialize method:
private static T Deserialize<T>(string data) where T : class, new()
{
if (string.IsNullOrEmpty(data))
return null;
var ser = new XmlSerializer(typeof(T));
using (var sr = new StringReader(data))
{
return (T)ser.Deserialize(sr);
}
}
I don't care about the other more parent elements such as 'ApplicationArea', 'Error' etc. I am only interesting in extracting the data within the 'Vehicle' element. How do I get it to only deserialize this data from the XML?
Building on Ilya's answer:
This can be optimized slightly, since there is already a loaded node: there is no need to pass the xml down to a string (using vehicle.ToString()), only to cause the serializer to have to re-parse it (using a StringReader).
Instead, we can created a reader directly using XNode.CreateReader:
private static T Deserialize<T>(XNode data) where T : class, new()
{
if (data == null)
return null;
var ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(data.CreateReader());
}
Or if data is a XmlNode, use a XmlNodeReader:
private static T Deserialize<T>(XmlNode data) where T : class, new()
{
if (data == null)
return null;
var ser = new XmlSerializer(typeof(T));
using (var xmlNodeReader = new XmlNodeReader(data))
{
return (T)ser.Deserialize(xmlNodeReader);
}
}
We can then use:
var vehicle = XDocument.Parse(xml)
.Descendants("Vehicle")
.First();
Vehicle v = Deserialize<Vehicle>(vehicle);
I'm not aware of the full context of your problem, so this solution might not fit into your domain. But one solution is to define your model as:
[XmlRoot("Vehicle")] //<-- optional
public class Vehicle
{
public string Colour { get; set; }
public string NumOfDoors { get; set; }
public string BodyStyle { get; set; }
}
and pass specific node into Deserialize method using LINQ to XML:
var vehicle = XDocument.Parse(xml)
.Descendants("Vehicle")
.First();
Vehicle v = Deserialize<Vehicle>(vehicle.ToString());
//display contents of v
Console.WriteLine(v.BodyStyle); //prints Hatchback
Console.WriteLine(v.Colour); //prints Blue
Console.WriteLine(v.NumOfDoors); //prints 3

Custom XML-element name for base class field in serialization

How can I change XML-element name for field inherited from base class while doing serialization?
For example I have next base class:
public class One
{
public int OneField;
}
Serialization code:
static void Main()
{
One test = new One { OneField = 1 };
var serializer = new XmlSerializer(typeof (One));
TextWriter writer = new StreamWriter("Output.xml");
serializer.Serialize(writer, test);
writer.Close();
}
I get what I need:
<?xml version="1.0" encoding="utf-8"?>
<One xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OneField>1</OneField>
</One>
Now I have created new class inherited from A with additional field and custom XML element name for it:
public class Two : One
{
[XmlElement("SecondField")]
public int TwoField;
}
Serialization code:
static void Main()
{
Two test = new Two { OneField = 1, TwoField = 2 };
var serializer = new XmlSerializer(typeof (Two));
TextWriter writer = new StreamWriter("Output.xml");
serializer.Serialize(writer, test);
writer.Close();
}
As a result I get next output:
<?xml version="1.0" encoding="utf-8"?>
<Two xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OneField>1</OneField>
<SecondField>2</SecondField>
</Two>
The problem is that I want to change OneField in this output to FirstField without touching base class code (because I will use it too and the names must be original). How can I accomplish this?
Thank you.
Try this:
public class Two : One
{
private static XmlAttributeOverrides xmlOverrides;
public static XmlAttributeOverrides XmlOverrides
{
get
{
if (xmlOverrides == null)
{
xmlOverrides = new XmlAttributeOverrides();
var attr = new XmlAttributes();
attr.XmlElements.Add(new XmlElementAttribute("FirstField"));
xmlOverrides.Add(typeof(One), "OneField", attr);
}
return xmlOverrides;
}
}
[XmlElement("SecondField")]
public string TwoField;
}
And your serializer init is a lot easier:
var xmls = new System.Xml.Serialization.XmlSerializer(typeof(Two), Two.XmlOverrides);
Here's a workaround: Override the fields in the subclass and mark the overriden field with whatever name you need. For example,
class One
{
public int OneField { get; set; }
}
class Two : One
{
[XmlElement("FirstField")]
public new int OneField
{
get { return base.OneField; }
set { base.OneField = value; }
}
}

XmlSerializer serialize generic List of interface

I'm trying to use the XmlSerializer to persist a List(T) where T is an interface. The serializer does not like interfaces. I'm curious if there is a simple way to serialize a list of heterogeneous objects easily with XmlSerializer. Here's what I'm going for:
public interface IAnimal
{
int Age();
}
public class Dog : IAnimal
{
public int Age()
{
return 1;
}
}
public class Cat : IAnimal
{
public int Age()
{
return 1;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var animals = new List<IAnimal>
{
new Dog(),
new Cat()
};
var x = new XmlSerializer(animals.GetType());
var b = new StringBuilder();
var w = XmlTextWriter.Create(b, new XmlWriterSettings { NewLineChars = "\r\n", Indent = true });
//FAIL - cannot serialize interface. Does easy way to do this exist?
x.Serialize(w, animals);
var s = b.ToString();
}
You can use XmlSerializer as well, but you need to include all the possible types that can appear in the object graph you're serializing, which limits extensibility and lowers maintainability. You can do it by using an overload of the constructor of XmlSerializer:
var x = new XmlSerializer(animals.GetType(), new Type[] { typeof(Cat), typeof(Dog) });
Also, there are several issues of note when using XmlSerializer, all of the outlined here (MSDN) - for example look under the heading 'Dynamically generated assemblies'.
The XmlSerializer can't handle an interface because it doesn't know which types to create when deserialising. To get around this you need to handle that part of the serialization yourself by implementing the IXmlSerializable interface. This allows you to record the type so you can re-create (deserialise) it.
The ListOfIAnimal class below shows how I inherited and extended the generic list List<IAnimal> to implement the required interface. I squished up your old classes adding an extra non-interface field to each so I could see that the concrete classes were getting serialised and deserialised properly.
Compared to your code I'm just using the new type ListOfIAnimal in place of List<IAnimal>, the other changes are just a little refactoring.
Its complete code, just copy it into it's own .cs file, call the first function to step through it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Serialiser
{
static class SerialiseInterface
{
public static void SerialiseAnimals()
{
String finalXml;
// Serialize
{
var animals = new ListOfIAnimal{
new Dog() { Age = 5, Teeth = 30 },
new Cat() { Age = 6, Paws = 4 }
};
var xmlSerializer = new XmlSerializer(animals.GetType());
var stringBuilder = new StringBuilder();
var xmlTextWriter = XmlTextWriter.Create(stringBuilder, new XmlWriterSettings { NewLineChars = "\r\n", Indent = true });
xmlSerializer.Serialize(xmlTextWriter, animals);
finalXml = stringBuilder.ToString();
}
// Deserialise
{
var xmlSerializer = new XmlSerializer(typeof(ListOfIAnimal));
var xmlReader = XmlReader.Create(new StringReader(finalXml));
ListOfIAnimal animals = (ListOfIAnimal)xmlSerializer.Deserialize(xmlReader);
}
}
}
public class ListOfIAnimal : List<IAnimal>, IXmlSerializable
{
public ListOfIAnimal() : base() { }
#region IXmlSerializable
public System.Xml.Schema.XmlSchema GetSchema() { return null; }
public void ReadXml(XmlReader reader)
{
reader.ReadStartElement("ListOfIAnimal");
while (reader.IsStartElement("IAnimal"))
{
Type type = Type.GetType(reader.GetAttribute("AssemblyQualifiedName"));
XmlSerializer serial = new XmlSerializer(type);
reader.ReadStartElement("IAnimal");
this.Add((IAnimal)serial.Deserialize(reader));
reader.ReadEndElement(); //IAnimal
}
reader.ReadEndElement(); //ListOfIAnimal
}
public void WriteXml(XmlWriter writer)
{
foreach (IAnimal animal in this)
{
writer.WriteStartElement("IAnimal");
writer.WriteAttributeString("AssemblyQualifiedName", animal.GetType().AssemblyQualifiedName);
XmlSerializer xmlSerializer = new XmlSerializer(animal.GetType());
xmlSerializer.Serialize(writer, animal);
writer.WriteEndElement();
}
}
#endregion
}
public interface IAnimal { int Age { get; set; } }
public class Dog : IAnimal { public int Age { get; set;} public int Teeth { get; set;} }
public class Cat : IAnimal { public int Age { get; set;} public int Paws { get; set;} }
}
I thought about leaving deserialize as an exercise for the reader, but the code would'n be very useful without it.
Do you have to use XmlSerializer? This is a known issue with XmlSerializer.
You can use BinaryFormatter to save to a stream:
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, animals);
Other alternative is to use WCF's DataContractSerializer and provide types using KnownType attribute.
You can use ExtendedXmlSerializer.
var serializer = new ExtendedXmlSerializer();
var xml = serializer.Serialize(animals);
Your xml will look like:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfIAnimal>
<Dog type="Model.Dog" />
<Cat type="Model.Cat" />
</ArrayOfIAnimal>
The easy way is to add the [Serializable()] decoration to your classes
and change your IList to List and see if that works.
If you use interfaces then go see webturner's answer.

Categories