VS.NET 2003, C# and .NET Framework 1.1.
Hello,
I am trying to serialize a small sample class using the XmlSerializer class.
** Here is the code:
DerivedClass myClass = new DerivedClass(5, "sample string");
Stream fs = File.Create("sample.xml");
XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8);
XmlSerializer serializer = new XmlSerializer(myClass.GetType());
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteComment("A comment here");
writer.WriteStartElement("root");
serializer.Serialize(writer, myClass);
writer.WriteEndElement();
writer.Close();
** Here is the exception I get:
System.IO.FileNotFoundException: File or assembly name rf3k0nh3.dll, or one
of its dependencies, was not found.
File name: "rf3k0nh3.dll"
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String
codeBase, Boolean isStringized, Evidence
assemblySecurity, Boolean throwOnFileNotFound, Assembly locationHint,
StackCrawlMark& stackMark)
at System.Reflection.Assembly.InternalLoad(AssemblyNa me assemblyRef,
Boolean stringized, Evidence
assemblySecurity, StackCrawlMark& stackMark)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef, Evidence
assemblySecurity)
at System.CodeDom.Compiler.CompilerResults.get_Compil edAssembly()
at System.CodeDom.Compiler.CompilerResults.get_Compil edAssembly()
at System.Xml.Serialization.Compiler.Compile()
at System.Xml.Serialization.TempAssembly..ctor(XmlMap ping[] xmlMappings)
at
System.Xml.Serialization.XmlSerializer.GenerateTem pAssembly(XmlTypeMapping
xmlTypeMapping)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String
defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at Serialization.Test.Create() in d:\sources\serialization\test.cs:line
147
at Serialization.Test.Main(String[] args) in
d:\sources\personal\serialization\test.cs:line 186
=== Pre-bind state information ===
LOG: Where-ref bind. Location =
C:\DOCUME~1\NOLDAN~1\LOCALS~1\Temp\rf3k0nh3.dll
LOG: Appbase = D:\Sources\Serialization\bin\Debug\
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: Policy not being applied to reference at this time (private, custom,
partial, or location-based assembly bind).
LOG: Attempting download of new URL
file:///C:/DOCUME~1/NOLDAN~1/LOCALS~1/Temp/rf3k0nh3.d.
** Here is the class to serialize:
[Serializable]
public class BaseClass
{
private string _name;
[XmlAttribute("name")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[XmlElementAttribute(IsNullable = false)]
public string emptyString;
[XmlIgnore]
[NonSerialized]
public string test;
public BaseClass()
{
_name = "";
test = "MyTest";
emptyString = "";
}
public BaseClass(string theStr)
{
_name = theStr;
test = "MyTest";
emptyString = "";
}
}
[Serializable]
[XmlRootAttribute("theClass", IsNullable = false)]
public class DerivedClass : BaseClass
{
private int _myInt;
[XmlElement("integer")]
public int MyInt
{
get { return _myInt; }
set { _myInt = value; }
}
public DerivedClass()
{
_myInt = 0;
}
public DerivedClass(int theInt, string theStr) :
base(theStr)
{
_myInt = theInt;
}
}
** The following code works correctly:
DerivedClass myClass = new DerivedClass(5, "sample string");
Stream fs = File.Create("sample.dat");
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(fs, myClass);
fs.Close();
Could you please tell me what's wrong in my code. Thank you.
--
Noël