Why is XmlSerializer throwing an InvalidOperationException? - c#

public void Save() {
XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation));
/*
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
*/
// ....
}
This is the whole class if you need it:
public class DatabaseInformation
{
/* Create new database */
public DatabaseInformation(string name) {
mName = name;
NeedsSaving = true;
mFieldsInfo = new List<DatabaseField>();
}
/* Read from file */
public static DatabaseInformation DeserializeFromFile(string xml_file_path)
{
XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation));
TextReader r = new StreamReader(xml_file_path);
DatabaseInformation ret = (DatabaseInformation)Serializer.Deserialize(r);
r.Close();
ret.NeedsSaving = false;
return ret;
}
/* Save */
public void Save() {
XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation));
if (!mNeedsSaving)
return;
TextWriter w = new StreamWriter(Path.Combine(Program.MainView.CommonDirectory.Get(), Name + ".xml"), false);
Serializer.Serialize(w, this);
w.Close();
NeedsSaving = false;
}
private string mName;
public string Name { get { return mName; } }
private bool mNeedsSaving;
public bool NeedsSaving { get { return mNeedsSaving; } set { mNeedsSaving = value; Program.MainView.UpdateTitle(value); } }
private bool mHasId;
public bool HasId { get { return mHasId; } }
List<DatabaseField> mFieldsInfo;
}
(PS: if you have any tips to improve my code feel free to share, I'm a C# beginner)

To serialize/deserialize your type it needs to have parameterless constructor. Check out here :
A class must have a default constructor to be serialized by
XmlSerializer.

oh.. I didn't know it had additional information (had to click "View detail.."), mystery solved:
Message=SDB.DatabaseInformation cannot
be serialized because it does not have
a parameterless constructor.

I was also getting this exception, but it wasn't due to missing a default constructor. I had some extra properties (a List and Dictionary) which aren't part of the XML document.
Decorating those properties with [XmlIgnore] solved the problem for me.

You can get around this by providing a default constructor that calls the overloaded constructor. For example:
public DatabaseInformation() : this ("defaultName"){}

Related

Serialization "Exception has been thrown by the target of an invocation."

Few days ago I've asked a question about object serialization and the answer was working well, actually it still works. But somehow I've copied the serialize function to an another class but didn't fit here and I have no idea why.
Exception has been thrown by the target of an invocation.
Inner Exception: An invalid argument was supplied.
Client.cs
public class Client
{
private string username;
public string Username
{
get { return username; }
set { username = value; }
}
private TcpClient tclient;
public TcpClient tClient
{
get { return tclient; }
set { tclient = value; }
}
public Client()
{}
public string Serialize(object obj)
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public object Deserialize(string json)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<object>(json);
}
}
Any ideas?
Since the TcpClient class is not a serializable class you can't simply serialize it. But as you can see here it has a constructor with (string: server, int: port) so you can add these fields to your class then after deserialization you can create it again.
Ps: It's funny that I was the one answered your previous question, here we are again.

Serializing an object to XML file throws an exception [duplicate]

This question already has answers here:
How to serialize non-static child class of static class
(3 answers)
Closed 7 years ago.
I've looked in several questions but none of the answers helped.
I tried using several stream objects (StreamWriter, FileStream).
I tried using XmlWriter, XmlSerializer and more.
This is my code:
namespace FacebookPlusPlus
{
internal static class AppConfig
{
public static string AccessToken
{
get { return s_SerializableConfig.m_AccessToken; }
set { s_SerializableConfig.m_AccessToken = value; }
}
public static bool AutoConnect
{
get { return s_SerializableConfig.m_AutoConnect; }
set { s_SerializableConfig.m_AutoConnect = value; }
}
public static string ErrorMessage { get; set; }
private const string k_ConfigFilePath = "AppConfig.xml";
private static SerializableConfig s_SerializableConfig = new SerializableConfig();
[Serializable]
public class SerializableConfig
{
public string m_AccessToken;
public bool m_AutoConnect;
}
public static bool ExportConfig()
{
bool exportSucceed = false;
try
{
using (StreamWriter writer = new StreamWriter(File.Open(k_ConfigFilePath, FileMode.Create)))
{
XmlSerializer serializer = new XmlSerializer(s_SerializableConfig.GetType());
serializer.Serialize(writer, s_SerializableConfig);
}
exportSucceed = true;
}
catch (Exception exception)
{
ErrorMessage = exception.Message;
if (File.Exists(k_ConfigFilePath))
{
File.Delete(k_ConfigFilePath);
}
}
return exportSucceed;
}
public static bool ImportConfig()
{
bool importSucceed = false;
if (File.Exists(k_ConfigFilePath))
{
try
{
using (Stream stream = File.Open(k_ConfigFilePath, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(s_SerializableConfig.GetType());
s_SerializableConfig = (SerializableConfig)serializer.Deserialize(stream);
}
importSucceed = true;
}
catch (Exception exception)
{
importSucceed = false;
ErrorMessage = exception.Message;
}
}
return importSucceed;
}
}
}
This is the exception:
There was an error generating the XML document.
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at FacebookPlusPlus.AppConfig.ExportConfig() in c:\\...\\AppLogic\\AppConfig.cs:line 48
At the time of error the field AccessToken contained a long string and AutoConnect contained true
OK, I did what #dbc suggested and made AppConfig public, removed its static attribute and concealed its c'tor. Works great!
Still am frustrated about C# weird limitations, this took me a couple of hours to understand. And I hate workarounds
namespace FacebookPlusPlus
{
public class AppConfig
{
...
[Serializable]
public class SerializableConfig
{
public string m_AccessToken;
public bool m_AutoConnect;
}
private AppConfig()
{
throw new InvalidOperationException("AppConfig Ctor Invoked");
}
...
}
}
When you call the Serealize method, the first parameter you are specifying may be the root cause of the issue:
In the xmlWriter parameter, specify an object that derives from the
abstract XmlWriter class. The XmlTextWriter derives from the
XmlWriter.
In your case, you are using StreamWriter which is not an XmlWriter.
Source: https://msdn.microsoft.com/en-us/library/10y9yyta(v=VS.110).aspx
Edit: Since you have already tried the above and it did not help with your problem, like Chris Sinclair said, try getting the inner exception. The following code snippet could help:
public void SerializeContainer( XmlWriter writer, Container obj )
{
try
{
// Make sure even the construsctor runs inside a
// try-catch block
XmlSerializer ser = new XmlSerializer( typeof(Container));
ser.Serialize( writer, obj );
}
catch( Exception ex )
{
DumpException( ex );
}
}
public static void DumpException( Exception ex )
{
Console.WriteLine( "--------- Outer Exception Data ---------" );
WriteExceptionInfo( ex );
ex = ex.InnerException;
if( null != ex )
{
Console.WriteLine( "--------- Inner Exception Data ---------" );
WriteExceptionInfo( ex.InnerException );
ex = ex.InnerException;
}
}
public static void WriteExceptionInfo( Exception ex )
{
Console.WriteLine( "Message: {0}", ex.Message );
Console.WriteLine( "Exception Type: {0}", ex.GetType().FullName );
Console.WriteLine( "Source: {0}", ex.Source );
Console.WriteLine( "StrackTrace: {0}", ex.StackTrace );
Console.WriteLine( "TargetSite: {0}", ex.TargetSite );
}
Source: https://msdn.microsoft.com/en-us/library/Aa302290.aspx

Deserialize json into C# object for class which has default private constructor

I need to deserialize json for following class.
public class Test
{
public string Property { get; set; }
private Test()
{
//NOTHING TO INITIALIZE
}
public Test(string prop)
{
Property = prop;
}
}
I can create an instance of Test like
var instance = new Test("Instance");
considering my json something like
"{ "Property":"Instance" }"
How shall I create an object of Test class as my default constructor is private and I am getting object where Property is NULL
I am using Newtonsoft Json parser.
You can make Json.Net call the private constructor by marking it with a [JsonConstructor] attribute:
[JsonConstructor]
private Test()
{
//NOTHING TO INITIALIZE
}
Note that the serializer will still use the public setters to populate the object after calling the constructor.
Another possible option is to use the ConstructorHandling setting:
JsonSerializerSettings settings = new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
Test t = JsonConvert.DeserializeObject<Test>(json, settings);
It doesn't seem like you need to take any extra steps.
Using Json.NET v6.0.8, the following C# program works inside LINQPad:
void Main()
{
var o = JsonConvert.DeserializeObject<Test>("{\"Property\":\"Instance\"}");
Debug.Assert(o.Property == "Instance",
"Property value not set when deserializing.");
}
public class Test
{
public string Property { get; set; }
private Test()
{
}
public Test(string propertyValue)
{
Property = propertyValue;
}
}
No need to create a Serializer setting and give assign ConstructorHandling here. Please remember to define the [JsonConstructor] attribute to the private constructor.
I have similar case with abstract BaseNode.cs and its concrete ComputerNode.cs implementation. You can create the classes, copy/paste the code below and do some experiment.
public abstract class BaseNode
{
[JsonConstructor] // ctor used when Json Deserializing
protected BaseNode(string Owner, string Name, string Identifier)
{
this.Name = Name;
this.Identifier = Identifier;
}
// ctor called by concrete class.
protected BaseNode(string [] specifications)
{
if (specifications == null)
{
throw new ArgumentNullException();
}
if (specifications.Length == 0)
{
throw new ArgumentException();
}
Name = specifications[0];
Identifier = specifications[1];
}
public string Name{ get; protected set; }
public string Identifier { get; protected set; }
}
public class ComputerNode: BaseNode
{
public string Owner { get; private set; }
[JsonConstructor] // not visible while creating object from outside and only used during Json Deserialization.
private ComputerNode(string Owner, string Name, string Identifier):base(Owner, Name, Identifier)
{
this.Owner = Owner;
}
public ComputerNode(string[] specifications):base(specifications)
{
Owner = specifications[2];
}
}
For JSon Read and Write following code helps -
public class Operation<T>
{
public string path;
public Operation()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "nodes.txt");
if (File.Exists(path) == false)
{
using (File.Create(path))
{
}
}
this.path = path;
}
public void Write(string path, List<T> nodes)
{
var ser = JsonConvert.SerializeObject(nodes, Formatting.Indented);
File.WriteAllText(path, ser);
}
public List<T> Read(string path)
{
var text = File.ReadAllText(path);
var res = JsonConvert.DeserializeObject<List<T>>(text);
return res;
}
}
All the best!
Today the short answer is: Rename the constructor parameter prop to property and your code will work fine.
public class Test
{
public string Property { get; }
public Test(string property)
{
Property = property;
}
}
Console.WriteLine(
JsonConvert.DeserializeObject(new Test("Instance")));
Newtonsoft.Json supports initializing properties using constructor parameters out of the box, without needing to set any additional attributes or changing any settings. The only constraint is that the parameter name needs to be a case insensitive match to the property name.
I discovered today that having a public constructor that takes parameters and no declared unparameterized constructor causes NewtonSoft to attempt to call the public constructor, the only one that it can find, since there is no explicit default constructor, and it cannot apparently find and call the default constructor provided by the framework unless it is the only constructor.
Explicitly declaring a default constructor causes NewtonSoft to call the correct (unparameterized) constructor.

Error when deserialize with XmlSerializer

I'm getting an exception with this code;
InnerException: System.InvalidOperationException
Message=The specified type was not recognized: name='Person',
namespace='', at "<"Contact xmlns=''>.
Here is the relevant code I think.
The class Person is a bare class without any anotations and doesn't inherit from any interface.
How to make Deserialize recognize my classes?
Thanks in advance.
public class Contacts : List<Contact.Contact>
{
private void PopulateTypeList()
{
types.Add(typeof(Contact.Contact));
types.Add(typeof(Contact.Company));
types.Add(typeof(Contact.Person));
types.Add(typeof(ContactData.Direction));
types.Add(typeof(ContactData.email));
types.Add(typeof(ContactData.Phone));
}
public void Load()
{
try
{
using (System.Xml.XmlReader stream = System.Xml.XmlReader.Create(fileName))
{
XmlSerializer xs = new XmlSerializer(typeof(List<Contact.Contact>));
// this roundabout way is for making it possible for this class to
// inherit from List<Contact.Contact> and still use a method that
// gives the stored data as an value in the object
here is error List<Contact.Contact> data =
(List<Contact.Contact>)xs.Deserialize(stream);
this.Clear();
this.AddRange(data);
}
}
catch (System.IO.FileNotFoundException)
{
// do nothing; no file, new database
}
}
public void Save()
{
using (System.Xml.XmlWriter stream = System.Xml.XmlWriter.Create(fileName))
{
XmlSerializer xs =
new XmlSerializer(typeof(List<Contact.Contact>), types.ToArray());
List<Contact.Contact> data = this.ToList();
xs.Serialize(stream, data);
}
}
Try passing the list of types to the serialiser you create for deserialisation:
XmlSerializer xs = new XmlSerialiser(typeof(List<Contact.Contact>), types.ToArray());

Generics + XML Serialization + Custom Objects

I'm trying out Generics and I had this (not so) great idea of creating an XMLSerializer class. The code I pieced together is below:
public class Persist<T>
{
private string _path;
public Persist(string path) {
this._path = path;
}
public void save(T objectToSave)
{
XmlSerializer s = new XmlSerializer(typeof(T));
TextWriter w = new StreamWriter(this._path);
try { s.Serialize(w, objectToSave); }
catch (InvalidDataException e) { throw e; }
w.Close(); w.Dispose();
}
public T load()
{
XmlSerializer s = new XmlSerializer(typeof(T));
TextReader r = new StreamReader(this._path);
T obj;
try { obj = (T)s.Deserialize(r); }
catch (InvalidDataException e) { throw e; }
r.Close(); r.Dispose();
return obj;
}
}
Here's the problem: It works fine on Persist<List<string>> or Persist<List<int>> but not on Persist<List<userObject>> or any other custom (but serializable) objects. userObject itself is just a class with two {get;set;} properties, which I have serialized before.
I'm not sure if the problems on my Persist class (generics), XML Serialization code, or somewhere else :( Help is very much appreciated~
Edit:
code for userObject
public class userObject
{
public userObject(string id, string name)
{
this.id = id;
this.name = name;
}
public string id { get;private set; }
public string name { get;set; }
}
Looks to me like your code should just work - even though it does have a few flaws.
EDIT: Your userObject class isn't serializable. Xml serialization only works on types with a public, parameterless constructor - the current class won't work. Also, you should really rewrite your code to avoid explicit calls to .Close() or .Dispose() and instead prefer using where possible - as is, you might get random file locking if at any point during serialization an error occurs and your method terminates by exception - and thus doesn't call .Dispose().
Personally, I tend to use a just-for-serialization object hierarchy that's just a container for data stored in xml and avoids any behavior - particularly side effects. Then you can use a handly little base class that makes this simple.
What I use in my projects is the following:
public class XmlSerializableBase<T> where T : XmlSerializableBase<T>
{
static XmlSerializer serializer = new XmlSerializer(typeof(T));
public static T Deserialize(XmlReader from) { return (T)serializer.Deserialize(from); }
public void SerializeTo(Stream s) { serializer.Serialize(s, this); }
public void SerializeTo(TextWriter w) { serializer.Serialize(w, this); }
public void SerializeTo(XmlWriter xw) { serializer.Serialize(xw, this); }
}
...which caches the serializer in a static object, and simplifies usage (no generic type-paramenters needed at call-locations.
Real-life classes using it:
public class ArtistTopTracks {
public string name;
public string mbid;//always empty
public long reach;
public string url;
}
[XmlRoot("mostknowntracks")]
public class ApiArtistTopTracks : XmlSerializableBase<ApiArtistTopTracks> {
[XmlAttribute]
public string artist;
[XmlElement("track")]
public ArtistTopTracks[] track;
}
Sample serialization calls:
using (var xmlReader = XmlReader.Create([...]))
return ApiArtistTopTracks.Deserialize(xmlReader);
//[...]
ApiArtistTopTracks toptracks = [...];
toptracks.SerializeTo(Console.Out);
There can be a number of reasons why your code fails: This text is particularly helpful when having issues: Troubleshooting Common Problems with the XmlSerializer . Maybe you have some type hierarchy in your user objects and the serializer does not know about it?

Categories

Resources