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);
}
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.