I have a class like this:
public class Data
{
public string Name { get; set; }
public int Size { get; set; }
public string Value { get; set; }
[NonSerialized] public byte[] Bytes;
}
When a List<Data> hits the serialization method below, it occasionally dies with
InvalidOperationException “This
XmlWriter does not support base64
encoded data.”
As you can see, I am not directly encoding anything, just using the default serialization mechanism.
private static XDocument Serialize<T>( T source )
{
var target = new XDocument( );
var s = new XmlSerializer( typeof( T ) );
using( XmlWriter writer = target.CreateWriter( ) )
{
s.Serialize( writer, source );
}
return target;
}
The data will have Name properties that are English words separated by underscores. The Value property will by similar except with added math operators or numbers (they are mathematical expressions).
Does anyone know what is causing it and how I can correct it?
Use
[XmlIgnore]instead of[NonSerialized]. The latter is for the SOAP and binary formatters, according to MSDN:Mind you, I’m surprised your original code even compiles – when I try it, it says that
[NonSerialized]can only be applied to fields…