|
-
May 13th, 2008, 09:11 AM
#1
XMLSerializer
Hello everyone,
Two questions about XMLSerializer.
http://msdn.microsoft.com/en-us/libr...er(VS.80).aspx
1.
It is mentioned -- "To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified
types."
What means the assemblies?
2.
For the following sample, why creates two XMLSerializer instances, one is s and the other is ser?
Code:
Hashtable serializers = new Hashtable();
// Use the constructor that takes a type and XmlRootAttribute.
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
// Implement a method named GenerateKey that creates unique keys
// for each instance of the XmlSerializer. The code should take
// into account all parameters passed to the XmlSerializer
// constructor.
object key = GenerateKey(typeof(MyClass), myRoot);
// Check the local cache for a matching serializer.
XmlSerializer ser = (XmlSerializer)serializers[key];
if (ser == null)
{
ser = new XmlSerializer(typeof(MyClass), myRoot);
// Cache the serializer.
serializers[key] = ser;
}
else
{
// Use the serializer to serialize, or deserialize.
}
thanks in advance,
George
-
May 13th, 2008, 01:39 PM
#2
Re: XMLSerializer
 Originally Posted by George2
It is mentioned -- "To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified
types."
This means an assembly is generated at the run-time which actually converts the Object into an XML stream. Read the whole article and you will understand what it means.
 Originally Posted by George2
What means the assemblies?
Assemblies in .NET on Wiki
Look at the code carefully and see what exactly is happening. Is there a Hashtable somewhere? Read the comments and also read the description and explanation that is given before and after the code on MSDN.
-
May 14th, 2008, 12:22 AM
#3
Re: XMLSerializer
Thanks Shuja,
Some further questions after study.
1.
You mean CLR will create a new assembly (DLL/EXE) for each type I need to serialize, other than using existing CLR assembly (e.g. System.XML.dll)?
For example, when I want to serialize type Foo, CLR will create something like FooSerialize.dll?
2.
The additional dynamically created assembly will be deleted when I unload the Appdomain? And next time when I run my assembly again, each of the dynamically created assemblies will be created again?
3.
My assembly will invoke the dynamically created assemblies to do serialize/deserialize?
4.
I think the following line of code is not necessary, agree? :-)
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
regards,
George
-
May 14th, 2008, 01:18 AM
#4
Re: XMLSerializer
 Originally Posted by George2
2.
For the following sample, why creates two XMLSerializer instances, one is s and the other is ser?
That code is useless. It's made by someone who doesn't understand how the .net serializer works. They think that they need to keep a copy of the serializer in the memory, but that is done by the framework.
"The making of software, like the making of sausages, should never be watched."
http://blog.gauffin.org - .NET Coding/Architecture
-
May 14th, 2008, 02:32 AM
#5
Re: XMLSerializer
 Originally Posted by George2
Thanks Shuja,
Some further questions after study.
1.
You mean CLR will create a new assembly (DLL/EXE) for each type I need to serialize, other than using existing CLR assembly (e.g. System.XML.dll)?
For example, when I want to serialize type Foo, CLR will create something like FooSerialize.dll?
2.
The additional dynamically created assembly will be deleted when I unload the Appdomain? And next time when I run my assembly again, each of the dynamically created assemblies will be created again?
3.
My assembly will invoke the dynamically created assemblies to do serialize/deserialize?
4.
I think the following line of code is not necessary, agree? :-)
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
regards,
George
What specific issues are you having? Is performance not hat you are expecting? What is the problem (other than theorectical)? As far as 4.... how else will the serializer for the object be defined? Have you tried coding it without? George please try these things BEFORE asking questions that you, yourself can answer.
-
May 14th, 2008, 07:25 AM
#6
Re: XMLSerializer
Thanks verifier,
Sorry my English is not very good. Do you mean the code with ser or s is not correct?
 Originally Posted by verifier
That code is useless. It's made by someone who doesn't understand how the .net serializer works. They think that they need to keep a copy of the serializer in the memory, but that is done by the framework.
regards,
George
-
May 14th, 2008, 07:27 AM
#7
Re: XMLSerializer
Thanks Arjay,
I already have some working code -- from functional perspective. But I want to learn more. From the MSDN page, I am confused about what means the additional assembly CLR will generate, which is something I do not know yet.
It contradicts with my previous understanding of how function call works, for example when we call function in System name space, we just call into System.dll, no need to generate any additional temporary DLL or something.
It is appreciated if you could clarify. :-)
 Originally Posted by Arjay
What specific issues are you having? Is performance not hat you are expecting? What is the problem (other than theorectical)? As far as 4.... how else will the serializer for the object be defined? Have you tried coding it without? George please try these things BEFORE asking questions that you, yourself can answer.
regards,
George
-
May 14th, 2008, 07:47 AM
#8
Re: XMLSerializer
 Originally Posted by George2
Thanks verifier,
Sorry my English is not very good. Do you mean the code with ser or s is not correct?
regards,
George
No problem. English is not my native language either.
The only thing you need is
Code:
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
You can throw away the rest.
You asked why you must supply both the type and the object (since the type can be retrieved by using myRoot.GetType()).
I think that the purpose is if you want to serialize just a base class and not the instance class (for example you want xml for User and not employee if myRoot is "class Employee : User").
"The making of software, like the making of sausages, should never be watched."
http://blog.gauffin.org - .NET Coding/Architecture
-
May 14th, 2008, 08:33 AM
#9
Re: XMLSerializer
 Originally Posted by George2
I already have some working code -- from functional perspective. But I want to learn more. From the MSDN page, I am confused about what means the additional assembly CLR will generate, which is something I do not know yet.
Serialization is something that works somewhat differently. When you serializae an object, the CLR builds a diferent assembly for that Class and serializes the object. This assembly is generated at run-time and subsequent serialization uses this dynamically generated assembly for the same classes. Basically when you use XMLSerializer, it actually generates some code that is compiled into an assembly dynamically and this assembly tranforms an Object into XML Stream.
 Originally Posted by George2
It contradicts with my previous understanding of how function call works, for example when we call function in System name space, we just call into System.dll, no need to generate any additional temporary DLL or something.
No it does not contradict with your previous understanding. Serialzing an object is different than doing a simple function call.
-
May 14th, 2008, 08:39 AM
#10
Re: XMLSerializer
 Originally Posted by Shuja Ali
. When you serializae an object, the CLR builds a diferent assembly for that Class and serializes the object. This assembly is generated at run-time .
Unless you set your project properties to generate XMLSerializer classes at Build Time.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 14th, 2008, 09:16 AM
#11
-
May 14th, 2008, 09:25 PM
#12
Re: XMLSerializer
Thanks Shuja Ali,
Your reply is very interesting. :-)
1.
The dynamically created assembly is another assembly other than my own assembly, which is dedicatedly working on Object to/from XML transformation?
2.
The additional dynamically created assembly will be deleted when I stop the process? And next time when I run my assembly again, each of the dynamically created assemblies will be created again?
3.
I read the MSDN sample from my original question post again, it seems that even if the process is running, when we create the same Serializer for the same type, if without optimization to keep the handle of the Serializer, the Serializer dynamically generated assembly will be created again?
(I mean they are never persistent to disk or something?)
 Originally Posted by Shuja Ali
Serialization is something that works somewhat differently. When you serializae an object, the CLR builds a diferent assembly for that Class and serializes the object. This assembly is generated at run-time and subsequent serialization uses this dynamically generated assembly for the same classes. Basically when you use XMLSerializer, it actually generates some code that is compiled into an assembly dynamically and this assembly tranforms an Object into XML Stream.
No it does not contradict with your previous understanding. Serialzing an object is different than doing a simple function call.
regards,
George
Last edited by George2; May 14th, 2008 at 09:30 PM.
-
May 14th, 2008, 09:28 PM
#13
Re: XMLSerializer
Thanks verifier,
After reading post #9 from Shuja Ali, I have some different options.
I think to make optimization, we should use the following code,
Code:
Hashtable serializers = new Hashtable();
// Implement a method named GenerateKey that creates unique keys
// for each instance of the XmlSerializer. The code should take
// into account all parameters passed to the XmlSerializer
// constructor.
object key = GenerateKey(typeof(MyClass), myRoot);
// Check the local cache for a matching serializer.
XmlSerializer ser = (XmlSerializer)serializers[key];
if (ser == null)
{
ser = new XmlSerializer(typeof(MyClass), myRoot);
// Cache the serializer.
serializers[key] = ser;
}
else
{
// Use the serializer to serialize, or deserialize.
}
and remove the following code from MSDN sample,
Code:
// Use the constructor that takes a type and XmlRootAttribute.
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
Any comments?
 Originally Posted by verifier
No problem. English is not my native language either.
The only thing you need is
Code:
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
You can throw away the rest.
You asked why you must supply both the type and the object (since the type can be retrieved by using myRoot.GetType()).
I think that the purpose is if you want to serialize just a base class and not the instance class (for example you want xml for User and not employee if myRoot is "class Employee : User").
regards,
George
-
May 14th, 2008, 09:31 PM
#14
Re: XMLSerializer
 Originally Posted by George2
Any comments?
When you tested this, what performance differences did you see?
-
May 14th, 2008, 09:58 PM
#15
Re: XMLSerializer
Thanks Arjay,
I only have two classes -- means 2 different types, there are not too many performance gains. But I have technical interests to learn something more, especially how to consider in the future when I have 200 classes. :-)
 Originally Posted by Arjay
When you tested this, what performance differences did you see?
regards,
George
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|