473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Xml serialization

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
Nov 15 '05 #1
3 2186
Sorry I forgot to mention that the exception is raised from the following
line:

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

Thank you.
--
Noël
Nov 15 '05 #2
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

Nov 15 '05 #3
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,
--
Noël
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.