Custom xml deserialization runs into an endless loop - c#

for our software I've to change the xml serialization for on entiy of our project.
After a couple of hours I've found, that you can modify the xml serialization if you implement the interface IXmlSerializable.
After some reading about how to implement this interface correctly, I've implemented this into the entity.
The entity which is implementing this interface is a child class. The parent class will be serialized into xml. If the child class is just a normal property, the serialization is correct and works like a charm.
But if the parent class has a list of this child classes, the serialization ends into an endless loop.
Here's a very simple example to illustrate my problem:
public class ParentEntity
{
public List<ChildEntity> Childs { get; set; } = new List<ChildEntity>();
}
public class ChildEntity: IXmlSerializable
{
public string Name { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
Name = reader.GetAttribute("Name");
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Name", Name);
}
}
And this is the logik I'm using for the serialization
using (var memoryStream = new MemoryStream())
{
var serializer = new XmlSerializer(typeof(ParentEntity));
serializer.Serialize(memoryStream, parentEntity);
memoryStream.Position = 0;
var deserializedParentEntity = serializer.Deserialize(memoryStream) as ParentEntity;
Console.Read();
}
The object parentEntity has been initialized correctly.
Then I'm serializing the object into xml.
After this, I'm deserializing the xml back into an object (Just for the showcase).
And here is my problem. The method ReadXml() get called very very often. It looks like a endless loop. The end of the deserialization process I've never reached.
Has anyone an idea what my problem is?

As explained in Proper way to implement IXmlSerializable, the method ReadXml() is responsible for reading the entire element including the <ChildEntity> start and end nodes, while WriteXml() should not do this as the wrapper element is written by the framework itself. If ReadXml() doesn't advance the reader past the initial element, you will get the infinite loop you are seeing.
Thus your ChildEntity type should look like:
public class ChildEntity : IXmlSerializable
{
public string Name { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
Name = reader.GetAttribute("Name");
reader.Skip(); // Consume the <ChildEntity> and </ChildEntity> (if present) nodes and everything contained therein.
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Name", Name);
}
}
Sample fiddle.

Related

XmlSerializer: deserialize recursive object graph

Given the following XML I want to deserialize:
<?xml version="1.0" encoding="utf-8" ?>
<units>
<entity>
<health max="1000"/>
<sprite texture="tank"/>
<entity>
<sprite texture="tank-turret"/> <!-- this element is missing when i deserialize --!>
</entity>
</entity>
</units>
How can I deserialize this recursive object graph using XmlSerializer?
The following is my last try. It successfully deserializes the top-level objects (health, sprite, entity) but it does not seem to find the sprite element in the nested entity node.
I also tried deriving entity from componentlist, but it didn't work either.
public class UnitSerializer
{
public abstract class item
{
}
public class entity : item
{
[XmlArray("entity")]
[XmlArrayItem(typeof(health))]
[XmlArrayItem(typeof(entity))]
[XmlArrayItem(typeof(sprite))]
public componentlist entity2 { get; set; }
}
public abstract class component : item
{
}
public class health : component
{
[XmlAttribute]
public int max { get; set; }
}
public class sprite : component
{
[XmlAttribute]
public string texture { get; set; }
}
public class componentlist : List<item>
{
}
[XmlRoot("units")]
public class units
{
[XmlArray("entity")]
[XmlArrayItem(typeof(health))]
[XmlArrayItem(typeof(entity))]
[XmlArrayItem(typeof(sprite))]
public componentlist entity { get; set; }
}
public void Read()
{
var x = new XmlSerializer(typeof(units),
new[] {
typeof(componentlist),
typeof(entity),
typeof(health),
typeof(sprite)
});
var fs = new FileStream("units.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(fs);
var units = (units)x.Deserialize(reader);
}
}
Your classes can be fixed by replacing use of [XmlArray] and [XmlArrayItem] with [XmlElement(typeof(TDerived))]:
public class UnitSerializer
{
public abstract class item
{
}
public class entity : item
{
[XmlElement("health", typeof(health))]
[XmlElement("entity", typeof(entity))]
[XmlElement("sprite", typeof(sprite))]
public List<item> EntityList { get; set; }
}
public abstract class component : item
{
}
public class health : component
{
[XmlAttribute]
public int max { get; set; }
}
public class sprite : component
{
[XmlAttribute]
public string texture { get; set; }
}
[XmlRoot("units")]
public class units
{
[XmlElement("health", typeof(health))]
[XmlElement("entity", typeof(entity))]
[XmlElement("sprite", typeof(sprite))]
public List<item> EntityList { get; set; }
}
public units Read(string filename)
{
var x = new XmlSerializer(typeof(units));
using (var fs = new FileStream(filename, FileMode.Open))
using (var reader = XmlReader.Create(fs))
{
return (units)x.Deserialize(reader);
}
}
}
Notes:
[XmlArray] indicates a collection should be serialized with an outer wrapper element containing a sequence of elements, while [XmlElement] indicates a collection should be serialized as a sequence without the wrapper. Your XML sample uses repeating elements without wrapper elements, so [XmlElement] should be used. It sort of works because your XML is recursive -- but at every other level repeating elements are getting incorrectly deserialized as wrappers. This explains why some but not all data is lost during deserialization.
In your XML sample, polymorphic elements are identified by element name. The XmlSerializer(Type, Type[]) constructor should be used to specify polymorphic included types to be serialized using the xsi:type mechanism. Since the xsi:type attribute does not appear in your XML, this constructor need not be used.
(In addition, when constructing an XmlSerializer using the XmlSerializer(Type, Type[]) constructor, you must cache the serializer statically to avoid a severe memory leak. See Memory Leak using StreamReader and XmlSerializer for why.)
XmlTextReader has been deprecated since .Net 2.0. Use XmlReader.Create() instead.
The FileStream and XmlReader should be disposed, ideally via using statements.
I eliminated the public class componentlist : List<item> and replaced it with just a List<item>. This was mainly a matter of taste, but it does make it easier to set the value of such a list using Linq's .ToList().
Demo fiddle here.

How to use XmlSerializer to serialize derived instances?

I realize this looks to be an exact duplicate of Using XmlSerializer to serialize derived classes, but I cannot figure out how to get this working following the guidance from that same question:
using System;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace xmlSerializerLab
{
public class Utf8StringWriter : System.IO.StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
[XmlRoot(ElementName = "Query", Namespace = "http://www.opengis.net/wfs")]
public class Query
{
[XmlElement(ElementName = "Filter", Namespace = "http://www.opengis.net/ogc")]
public Filter Filter { get; set; }
}
[XmlInclude(typeof(PropertyIsOpFilter))]
[XmlInclude(typeof(PropertyIsEqualToFilter))]
[XmlInclude(typeof(OpFilterBase))]
[XmlInclude(typeof(LiteralFilter))]
[XmlInclude(typeof(Query))]
[Serializable]
public class Filter
{
[XmlElement]
public Filter And { get; set; }
}
public class PropertyIsOpFilter : Filter, IXmlSerializable
{
public Filter LeftOp { get; set; }
public Filter RightOp { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader) { }
public void WriteXml(XmlWriter writer)
{
Program.ToXml(LeftOp, writer);
Program.ToXml(RightOp, writer);
}
}
[XmlRoot("IsEqualTo")]
public class PropertyIsEqualToFilter : PropertyIsOpFilter { }
public class OpFilterBase : Filter, IXmlSerializable
{
public string Op { get; set; }
public object Value { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader) { }
public void WriteXml(XmlWriter writer)
{
if (!String.IsNullOrEmpty(Op))
{
writer.WriteStartElement(Op);
writer.WriteValue(Value);
writer.WriteEndElement();
}
else
{
writer.WriteValue(Value);
}
}
}
public class LiteralFilter : OpFilterBase { }
class Program
{
public static void ToXml(Object o, XmlWriter writer)
{
var inputSerializer = new XmlSerializer(o.GetType(), new Type[] {
typeof(Filter),
typeof(PropertyIsOpFilter),
typeof(PropertyIsEqualToFilter),
typeof(OpFilterBase),
typeof(LiteralFilter),
typeof(Query)
});
inputSerializer.Serialize(writer, o);
}
public static string ToXml(Object o)
{
var inputSerializer = new XmlSerializer(o.GetType());
using (var writer = new Utf8StringWriter())
{
using (var xmlWriter = new XmlTextWriter(writer))
{
ToXml(o, xmlWriter);
}
return writer.ToString();
}
}
static void Main(string[] args)
{
Filter o = new PropertyIsEqualToFilter()
{
LeftOp = new LiteralFilter()
{
Value = 1
},
RightOp = new LiteralFilter()
{
Value = 1
}
};
var query = new Query()
{
Filter = o
};
Console.WriteLine(ToXml(query));
Console.ReadLine();
}
}
}
It results in this exception:
InvalidOperationException: The type
xmlSerializerLab.PropertyIsEqualToFilter may not be used in this
context. To use xmlSerializerLab.PropertyIsEqualToFilter as a
parameter, return type, or member of a class or struct, the parameter,
return type, or member must be declared as type
xmlSerializerLab.PropertyIsEqualToFilter (it cannot be object).
Objects of type xmlSerializerLab.PropertyIsEqualToFilter may not be
used in un-typed collections, such as ArrayLists.
As far as I can tell, I need the IXmlSerializable on the PropertyIsOpFilter and OpFilterBase because I'm trying to target a specific XML format described by this schema. But I'm finding that I also have to make the Query class IXmlSerializable.
Here is a sample XML document that I'd like to be able to produce from the model:
<GetFeature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
service="WFS"
version="1.1.0"
maxFeatures="0" xmlns="http://www.opengis.net/wfs">
<ResultType>Results</ResultType>
<OutputFormat>text/gml; subtype=gml/3.1.1</OutputFormat>
<Query
d2p1:srsName="EPSG:4326" xmlns:d2p1="http://www.opengis.net/ogc">
<d2p1:Filter>
<d2p1:IsEqualTo>
<d2p1:PropertyName>Prop1</d2p1:PropertyName>
<d2p1:Literal>1</d2p1:Literal>
</d2p1:IsEqualTo>
</d2p1:Filter>
</Query>
</GetFeature>
By making the Query class IXmlSerializable and writing a good bit of WriteXml and ReadXml logic I can get it to work, but I'd expect it to work without having to do all that since the XmlRoot and XmlAttribute and XmlElement tags should give enough information to the serializer for it to know which class to instantiate based on the tag name (match ElementName) and certainly how to serialize based on the attributes.
The problem you are seeing can be reproduced with the following minimal example:
public class BaseClass
{
}
public class DerivedClass : BaseClass, IXmlSerializable
{
#region IXmlSerializable Members
public XmlSchema GetSchema() { return null; }
public void ReadXml(XmlReader reader) { throw new NotImplementedException(); }
public void WriteXml(XmlWriter writer) { }
#endregion
}
Using the serialization code:
BaseClass baseClass = new DerivedClass();
using (var textWriter = new StringWriter())
{
using (var xmlWriter = XmlWriter.Create(textWriter))
{
var serializer = new XmlSerializer(typeof(BaseClass), new Type[] { typeof(DerivedClass) });
serializer.Serialize(xmlWriter, baseClass);
}
Console.WriteLine(textWriter.ToString());
}
The following exception is thrown (sample fiddle #1):
System.InvalidOperationException: There was an error generating the XML document.
---> System.InvalidOperationException: The type DerivedClass may not be used in this context. To use DerivedClass as a parameter, return type, or member of a class or struct, the parameter, return type, or member must be declared as type DerivedClass (it cannot be object). Objects of type DerivedClass may not be used in un-typed collections, such as ArrayLists.
This is among the most unhelpful exception messages I have seen from XmlSerializer. To understand the exception, you need to understand how XmlSerializer handles polymorphism via the [XmlInclude] mechanism. If I remove IXmlSerializable from DerivedClass, the following XML is generated (fiddle #2):
<BaseClass xsi:type="DerivedClass" />
Notice the xsi:type type attribute? That is a w3c standard attribute that XmlSerializer uses to explicitly assert the type of a polymorphic element; it is documented here. When XmlSerializer is deserializing a polymorphic type to which [XmlInclude] attributes have been applied (either statically or through the constructor you are using), it will look for the xsi:type attribute to determine the actual type to construct and deserialize.
It is this, apparently, which conflicts with IXmlSerializable. A type which implements this interface should completely control its XML reading and writing. However, by parsing and interpreting the xsi:type attribute, XmlSerializer has already begun automatic deserialization, and so throws an exception due to the inconsistent deserialization strategies of the base and derived types.
What's more, adding IXmlSerializable to the base type doesn't really fix the problem either If you do so, the xsi:type attribute is never written, and later, when ReadXml() is called, an object of the base type will get unconditionally constructed, as shown in fiddle #3.
(It's conceivable that Microsoft could have implemented a special case where XmlSerializer begins automatic deserialization, then "backs off" and hands the task over to ReadXml() when an IXmlSerializable polymorphic type is encountered and constructed. But, they did not.)
The solution would seem to be to serialize your Filter types automatically using the [XmlInclude] mechanism. In fact I don't see any reason you need to use IXmlSerializable, and was able to serialize your model successfully by removing IXmlSerializable completely and making some minor changes to namespaces:
public static class XmlNamespaces
{
public const string OpengisWfs = "http://www.opengis.net/wfs";
}
[XmlRoot(Namespace = XmlNamespaces.OpengisWfs)]
public class Query
{
public Filter Filter { get; set; }
}
[XmlInclude(typeof(PropertyIsOpFilter))]
[XmlInclude(typeof(OpFilterBase))]
[XmlRoot(Namespace = XmlNamespaces.OpengisWfs)]
public class Filter
{
[XmlElement]
public Filter And { get; set; }
}
[XmlInclude(typeof(PropertyIsEqualToFilter))]
[XmlRoot(Namespace = XmlNamespaces.OpengisWfs)]
public class PropertyIsOpFilter : Filter
{
public Filter LeftOp { get; set; }
public Filter RightOp { get; set; }
}
[XmlRoot("IsEqualTo", Namespace = XmlNamespaces.OpengisWfs)]
public class PropertyIsEqualToFilter : PropertyIsOpFilter { }
[XmlInclude(typeof(LiteralFilter))]
[XmlRoot(Namespace = XmlNamespaces.OpengisWfs)]
public class OpFilterBase : Filter
{
public string Op { get; set; }
public object Value { get; set; }
}
[XmlRoot(Namespace = XmlNamespaces.OpengisWfs)]
public class LiteralFilter : OpFilterBase { }
Notes:
For the [XmlInclude] mechanism to work, all the included types apparently must be in the same XML namespace as the base type. To ensure this I added [XmlRoot(Namespace = XmlNamespaces.OpengisWfs)] to all the Filter subtypes.
The [XmlInclude(typeof(DerivedType))] attributes can be added either to their immediate parent type or to the lowest common base type. In the code above I added the attributes to the immediate parent types so that members of an intermediate type could be serialized successfully, e.g.:
public class SomeClass
{
PropertyIsOpFilter IsOpFilter { get; set; }
}
Consider marking intermediate types that cannot be instantiated as abstract, e.g. public abstract class Filter. Consider marking types that are "most derived" as sealed, e.g. public sealed class LiteralFilter
If you use the new XmlSerializer(Type, Type []) constructor, you must statically cache the serializer to avoid a severe memory leak, as explained here. It's not necessary in my solution but you are using it in your question.
Sample fiddle #4 showing that the following XML is generated successfully:
<Query xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs">
<Filter xsi:type="PropertyIsEqualToFilter">
<LeftOp xsi:type="LiteralFilter">
<Value xsi:type="xsd:int">1</Value>
</LeftOp>
<RightOp xsi:type="LiteralFilter">
<Value xsi:type="xsd:int">1</Value>
</RightOp>
</Filter>
</Query>

CSV Serialization of inherited types

I am attempting to serialise some records into CSV using the ServiceStack.Text library.
I am using inheritance, specifically abstract classes and the properties on the child types are not being output. Yes I know this is a bad idea but as I have no need to deserialise the types and I'm not making a public API. Regardless, this scenario seems to be supported by the docs.
For this example:
public abstract class ResultBase
{
public int MinuteOffset { get; set; }
public double SegmentDuration { get; set; }
}
public class EventIndex : IndexBase
{
public int EventsTotal { get; set; }
public int EventsTotalThresholded { get; set; }
}
And the code to serialise:
var destination = new FileInfo("C:\\somefile.txt")
using (var stream = destination.CreateText())
{
JsvStringSerializer s = new JsvStringSerializer();
var o = s.SerializeToString(results);
stream.Write(o);
CsvSerializer.SerializeToWriter(results, stream);
}
The CSV serialiser outputs this (not what I want):
MinuteOffset,SegmentDuration
0,0
But, the JSV serialiser seems to behave as expected:
[{__type:"AnalysisBase.EventIndex, AnalysisBase",EventsTotal:0,EventsTotalThresholded:0,MinuteOffset:0,SegmentDuration:0}]
Why are there differences in the fields output, is there anyway I can get the CSV serialiser to output all child properties?
The CsvSerializer.SerializeToWriter is a generic method which is not operating on the runtime type of the object. If you are calling for serialization through a base type of the current instance then the serializer will not now any other properties then the base one's.
public static void SerializeToWriter<T>(T value, TextWriter writer)
{
if (value == null) return;
if (typeof(T) == typeof(string))
{
writer.Write(value);
return;
}
CsvSerializer<T>.WriteObject(writer, value);
}

Pass List<child> as argument?

I want to create a method that serializes lists of different childs. Each child gets his own XML file. Doing this for a single object was easy:
interface ISerializable
{
}
public class Item : ISerializable
{
public string name { get; set; }
public int number { get; set; }
}
public class Weapon : ISerializable
{
public string name { get; set; }
public int damage {get; set;}
}
public static void SerializeToXML(ISerializable child)
{
XmlSerializer serializer = new XmlSerializer(child.GetType());
using (TextWriter textWriter = new StreamWriter("test.xml"))
{
serializer.Serialize(textWriter, child);
}
}
I can put anything derived from ISerializable into the serialize method to get a desired result. However when give List<ISerializable> as possible argument it does not compile Cannot convert List<item> to List<ISerializable>. Anyway to solve this?
public static void SerializeListToXML(List<ISerializable> listToXML)
{
XmlSerializer serializer = new XmlSerializer(listToXML.GetType());
using (TextWriter textWriter = new StreamWriter("test.xml"))
{
serializer.Serialize(textWriter, listToXML);
}
}
The base reason i'm doing this is to get XML templates of much larger structures so i an manually add XML to load into my program. I figured i would benefit from creating a serializer first so i have the correct XML structure to start with. Perhaps this can come in handy later down the line for saving user data too.
SerializeListToXML should take an IEnumerable<ISerializable> instead of a List<Item>.
A List<Item> cannot be casted to List<ISerializable>. If you could to that, then you'd be able to add any ISerializable to the list, not just Items. With an IEnumerable<>, you cannot modify the collection - only iterate over it. Since SerializeListToXML does not need to modify the collection, it can accept an IEnumerable<ISerializable>, which is less restrictive. Please see the following section and sub-sections about covariance and contravariance: Covariance and Contravariance

error generating xml document in C#

I'm trying to so some serialization using C#, but it throws an exception saying that there was an error generating the xml document. This is where I do the serialization:
public void serialize()
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(Repository<Student>));
StreamWriter myWriter = new StreamWriter("stud.xml");
ser.Serialize(myWriter, rep);
myWriter.Close();
}
catch (Exception e)
{
Console.WriteLine("Error " + e.Message);
}
}
And this is the class I want to serialize:
public class Repository<T> : MyStack<T>
{
public int size;
public int capacity;
public SLL<T> stud;
public Repository()
{
/*
* Creator for class Repository.
*/
this.stud = new SLL<T>();
this.capacity = 20;
this.size = 0;
}
where MyStack is an interface, and SSL<T> is singly-linked list I have implemented.
Please add the full details of your exception to isolate the problem. From the code that you posted, it looks like your classes are not serializable.
Are the Repository, MyStack, SSL serializable classes (do you have the [Serializable] attribute on all the types of the members that will be serialized?)
It looks odd to serialise the repository class. Instead I would serialise the SLL<T> object, since that is the actual data you are interested in. The repository is just wrapper object for getting to data, thus there's no reason to serialise that.
As Cosmin mentions, make sure you place a [Serializable] attribute on SLL<>
[Serializable]
public class SLL<T>
{
}
Here is an example how to serialize. i think you might miss some important parts:
[Serializable]
public class User
{
[XmlElement("login")]
[Key]
public string login { get; set; }
[XmlElement("KDP")]
public int KDP { get; set; }
[XmlElement("attended")]
public int attended { get; set; }
etc.
the public getters and setters are important if you ever plan to deserialize.
and here is example how to serialize list of these:
[XmlArrayItem(typeof(User))]
[XmlElement("usersList")]
public static List<User> usersList = new List<User>();
using (StreamWriter myWriter = new StreamWriter(usersPath, false))
{
userSerializer.Serialize(myWriter, usersList);
myWriter.Close();
}
hope it helps.
Use [XmlIgnore] attribute for the property which is causing error generating the xml document, so that other things can be parsed.

Categories

Resources