XmlSerializer - There was an error reflecting type
Look at the inner exception that you are getting. It will tell you which field/property it is having trouble serializing.
You can exclude fields/properties from xml serialization by decorating them with the [XmlIgnore]
attribute.
XmlSerializer
does not use the [Serializable]
attribute, so I doubt that is the problem.
XmlSerializer - There was an error reflecting type. Two classes with the same name
After having lost enough time for me, I can end with this saying that It's (almost) impossible (nothing is impossible). I tried also with the ISerializable interface implementation.
There was an error reflecting type - XML Serialization issue
Guys.. With a little help on web I found a solution..
I had to add another class
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue>: Dictionary<TKey, TValue>, IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
return;
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
reader.ReadStartElement("key");
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
TValue value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
this.Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}
Then used the SerializableDictionary object instead of normal Dictionary. Also in my CommonControls class I had to implement "IXmlSerializable" with following methods.
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(XmlWriter writer)
{
throw new NotImplementedException();
}
#endregion
Now the whole thing is working ok. Thanks Everyone. !!!
XMLSerializer exceptions There was an error reflecting field and For non-array types, you may use the following attributes: XmlAttribute, ..
[XmlRoot("SQLMultiFilter", Namespace = "http://www.cpandl.com", IsNullable = false)]
public class SqlMultiFilter
{
public List<Filter> Parameter { get; set; }
}
public class Filter
{
public string ParamName;
[XmlArrayItem("ParamValue")]
public List<string> ParamValues { get; set; }
}
These two classes will allow you to reproduce the XML of the schema shown.
var serializer = new XmlSerializer(typeof(SqlMultiFilter));
var sqlMultiFilter = new SqlMultiFilter
{
Parameter = new List<Filter>
{
new Filter { ParamName = "computerid", ParamValues = new List<string> { "123456" } },
new Filter { ParamName = "computername", ParamValues = new List<string> { "mycomputer" } },
new Filter { ParamName = "computermodel", ParamValues = new List<string> { "mymodel" } }
}
};
var settings = new XmlWriterSettings { Indent = true };
using (var xmlWriter = XmlWriter.Create(Console.Out, settings))
serializer.Serialize(xmlWriter, sqlMultiFilter);
Will give the desired result.
There was an error reflecting property 'Lists'
[XmlArray("Lists")]
[XmlArrayItem("List")]
public virtual List<object> Lists { get; set; }
and add mark knownTypeAttribute with serializable types as object
Related Topics
Why Do Members of a Static Class Need to Be Declared as Static? Why Isn't It Just Implicit
Redirecting Output to the Text File C#
How to Implement Automatic Sorting of Datagridview
Xml Validation Using Xsd Schema
Mvvm Light & Wpf - Binding Multiple Instances of a Window to a Viewmodel
Preprocessor Directive in C# for Importing Based on Platform
How to Take the Cartesian Join of Two Lists in C#
Convert Am/Pm Time to 24 Hours Format
Newtonsoft JSON.Net Serialize Jobject Doesn't Ignore Nulls, Even with the Right Settings
C# Object Initialization of Read Only Collection Properties
A Dictionary Object That Uses Ranges of Values for Keys
How to Include Other Files to the Output Directory in C# Upon Build