When seralizing a class and saving to a file, sometimes an error occurs where the serialized output looks like this:
<?xml version="1.0"?>
<Template xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Route>Some Route</Route>
<TradePack>Something Here</TradePack>
<Transport />
</Template>te> ------> Notice this extra string?
The class I'm serializing looks like this:
[Serializable]
public class Template
{
public string Route = string.Empty;
public string TradePack = string.Empty;
public string Transport = string.Empty;
public Template()
{
}
}
I can't seem to figure out why this is happening. Here's my serializer class:
public static bool Save(object obj, string path)
{
try
{
XmlSerializer writer = new XmlSerializer(obj.GetType());
using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
writer.Serialize(stream, obj);
}
return true;
}
catch { }
return false;
}
Thanks!