cannot use xmlSerializer with FileSystemInfo
I am having difficulty getting FileSystemInfo to work with the xmlSerializer. The snippet is pasted below. At runtime an error occurs stating 'System.IO.DirectoryInfo cannot be serialized because it does not have a parameterless constructor.' I have tried working around this in several ways: explicit casting of DirectoryInfo; a wrapper object (snippet shown below line) that is set to fsi. The object won't cast to FileSystemInfo.
Any help or suggestions would be much appreciated. I can be contacted at bedocs@gdls.com.
Stream stream = new FileStream(fileName,FileMode.Create,FileAccess.Write,FileShare.None);
FileSystemInfo fsi = new DirectoryInfo(path);
try
{
xmlSerialize = new XmlSerializer(fsi.GetType());
_
publicclassDirectoryData:FileSystemInfo
{
public DirectoryData()
{
}
[1147 byte] By [
Joe0532] at [2007-12-24]
At times, I've gotten around issues like this, by inserting the object in a dictionary and serializing the dictionary. There are some times that this absolutely will not work such as when I've attempted to use it to serialize a toolstripmenu button. But you might try it. Also note this was with binary serialization and not XML.
DirectoryInfo is serializable by the BinaryFormatter (aka Runtime Serializer) and not the XmlSerializer. If you want it to be serialized by XmlSerializer you may need to wrap the DirectoryInfo class and serve up each public property of DirectoryInfo as a property of the wrapper class.
Unfortunately the Dictionary approach would not work either since Dictionaries are not serializable by XmlSerializer (but are serializable by the BinaryFormatter)
Also the right alias to post XmlSerializer question is http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=46&SiteID=1
I hope this helps