Xml serialization

N

Noël Danjou

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(AssemblyName assemblyRef,
Boolean stringized, Evidence
assemblySecurity, StackCrawlMark& stackMark)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef, Evidence
assemblySecurity)
at System.CodeDom.Compiler.CompilerResults.get_CompiledAssembly()
at System.CodeDom.Compiler.CompilerResults.get_CompiledAssembly()
at System.Xml.Serialization.Compiler.Compile()
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings)
at
System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(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.
 
N

Noël Danjou

Sorry I forgot to mention that the exception is raised from the following
line:

XmlSerializer serializer = new XmlSerializer(myClass.GetType());

Thank you.
 
S

Steven Cheng[MSFT]

Hi,


Thanks for posting in the community!
From your description, you encountered some certain exceptions when
serializing some custom classes, yes?
If there is anything I misunderstood, please feel free to let me know.

From the exceptions and call stacks you provided, it seems not quite
obvious on what's the root cause. So I copied your classes and seriailizing
code and had a test on my side and found that the cause of the problem is
via the XmlAttributes's typed style you've applied on the classed, here are
the code block which have the problem:

[XmlElementAttribute(IsNullable = false)]
public string emptyString;

[Serializable]
[XmlRootAttribute("theClass", IsNullable = false)]
public class DerivedClass : BaseClass
{

In fact, all the XmlAttributes such as XmlAttributeAttribute ,
XmlElementAttribute , XmlArrayAttribute ...., when we applying them on some
certain classes, we should just type their name without the "Attribute" ,
for example, below is the correct format:

[XmlElement(IsNullable = false)]
public string emptyString;

[Serializable]
[XmlRoot("theClass", IsNullable = false)]
public class DerivedClass : BaseClass
{

After changing to this, your code worked perfect well and here are the
modified classes code and serilizing code:
-------------------------classes code------------------------------
[Serializable]
public class BaseClass
{
private string _name;

[XmlAttribute("name")]
public string Name
{
get { return _name; }
set { _name = value; }
}

[XmlElement(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]
[XmlRoot("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;
}
}
----------------------------------------------------------------------------
-----------------
----------------------------serialize
code---------------------------------------
XmlSerializer serializer = new XmlSerializer(typeof(DerivedClass));

DerivedClass dc = new DerivedClass(100,"MyDerivedClass");
dc.emptyString = "derived empty string";
dc.test = "derived test string";

Stream fs = File.Create("xmlclasses.xml");
XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteComment("A comment here");
writer.WriteStartElement("root");
serializer.Serialize(writer, dc);
writer.WriteEndElement();
writer.Close();

Please check out the above results. If you have any further questions,
please feel free to post here.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
N

Noël Danjou

Hi Steven,

Thanks a lot for your reply and your useful information.

I modified the attributes like you described but I was still getting the
same exception. I created a new test project, copied and pasted just that
part of the code and yet I was still getting the same exception.

At last I figured out the issue: I was using "Serialization" as the
namespace for my sample code, this was actually a bad idea. Now I changed
the namespace for my whole sample application and the serialization works
just fine!

Thanks for your support.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top