I am having a problem using XML Serialization.
I have a class that serializes an object and saves it to disk, (sort of a
page file system for a web app).
You pass in the object, and the serialization method to use and it will
serialize it to disk for later retrievel.
The following scenarios work great:
1. Serializing anything with Binary Serialization
2. Serializing any type in the System namespace with XML Serialization
The following scenario is where I am having difficulty:
3. Serializing any type not in the System namespace (ie System.Data.Dataset)
The problem actually occurs in the deserialization when I am trying to
retrieve the type of the object to pass it to the constructor of the
XmlSerializer object.
Here is the code that is giving me trouble:
Type oType = Type.GetType(oNode.Attributes["type"].InnerText,true, true);
For example, when I am trying to deserialize a Dataset object, the InnerText
property of this node is set to "System.Data.Dataset", and I receive the
following exception at that line of code:
System.TypeLoadException : Could not load type System.Data.DataSet from
assembly PageFileManager
(PageFileManager is the calling class)
So my two questions are:
1. Am I missing a step here where I need to use reflection to load some
assembly.
or
2. Is there a way to deserialize XML without specifying the type in the
constructor of the XmlSerializer object?
Thanks in advance,
Lee
Lee - 29 Oct 2004 15:33 GMT
I figured this out, in case anyone else has this problem here is the key:
Originally I was doing this to save the type information:
oNode.Attributes["type"].InnerString = oObjectType.ToString();
So in the case of System.Data.Dataset, It would save "System.Data.Dataset".
I changed the above line to this:
oNode.Attributes["type"].InnerString = oObjectType.AssemblyQualifiedName;
And this would save:
"System.Data.DataSet, System.Data, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
Then the subsequent call to Type.GetType() was able to load the type from
this fully qualified name.
Maybe this was obvious to other people, but it wasn't to me. However after
thinking about it, it wouldn't make sense for the CLR to keep track of all of
the System dlls and their types.
Lee