How can I use IEnumerable interface and at the same time, have XMLSerializer not use GetEnumerator()?
By : Yuks
Date : March 29 2020, 07:55 AM
seems to work fine I managed to fix this problem without having to change my design. None of my code was relying on the IEnumerable interface, just the implementation of IEnumerable GetEnumerator() (apparently foreach doesn't check to see if IEnumerable is implemented). Just commenting out the interface in the class declaration did the trick.
|
XmlSerializer and IEnumerable: Serialization possible w/o parameterless constructor: Bug?
By : user2641024
Date : March 29 2020, 07:55 AM
To fix this issue The reason for the behavior is that this is the way it has always worked. From XmlSerializer class:
|
XmlSerializer - IEnumerable or IList as LIst
By : merbng
Date : March 29 2020, 07:55 AM
it fixes the issue Since you can't change the class, create a new class, inheriting from the class you want to serialize and from IXmlSerializable. In addition, you can override the base Colors array with the new keyword. Try this one: code :
public class Something
{
public int Id { get; set; }
public string Text { get; set; }
public IEnumerable<string> Colors { get; set; }
}
public class MySerializableSomething : Something, IXmlSerializable
{
public new List<string> Colors { get; set; }
public MySerializableSomething()
{
Colors = new List<string>();
}
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
while (reader.Read())
{
switch (reader.LocalName)
{
case "Id": Id = reader.ReadElementContentAsInt(); break;
case "Text": Text = reader.ReadElementContentAsString(); break;
case "Color": Colors.Add(reader.ReadElementContentAsString()); break;
}
}
}
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("Id", Id.ToString());
writer.WriteElementString("Text", Text);
writer.WriteStartElement("Colors");
foreach (var color in Colors)
{
writer.WriteElementString("Color", color);
}
writer.WriteEndElement();
}
}
|
XmlSerializer not deserializing XML to IEnumerable
By : user3541820
Date : March 29 2020, 07:55 AM
To fix this issue When your XML-serialized type has a property of type List, XmlSerializer will get the value of the property, then call .Add(...) to add the deserialized elements (i.e. it does not set the property). Since the getter for the GlossarySurrogate property returns a new list every time you get it, the changes the XML serializer makes to the result are lost.
|
XmlSerializer won't serialize IEnumerable
By : Domi200
Date : March 29 2020, 07:55 AM
Any of those help The way you serialize an IEnumerable property is with a surrogate property
|