CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: XMLSerializer

  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: XMLSerializer

    Quote 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.

    Quote 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.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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

  4. #4
    Join Date
    Jun 2002
    Location
    Sweden
    Posts
    467

    Re: XMLSerializer

    Quote 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

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: XMLSerializer

    Quote 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.

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: XMLSerializer

    Thanks verifier,


    Sorry my English is not very good. Do you mean the code with ser or s is not correct?

    Quote 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

  7. #7
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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. :-)

    Quote 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

  8. #8
    Join Date
    Jun 2002
    Location
    Sweden
    Posts
    467

    Re: XMLSerializer

    Quote 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

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: XMLSerializer

    Quote 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.

    Quote 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.

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: XMLSerializer

    Quote 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

  11. #11
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: XMLSerializer

    Quote Originally Posted by TheCPUWizard
    Unless you set your project properties to generate XMLSerializer classes at Build Time.
    You can also use SGEN.EXE tool.

    Actually this was in response George's question

  12. #12
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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?)

    Quote 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.

  13. #13
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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?

    Quote 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

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: XMLSerializer

    Quote Originally Posted by George2
    Any comments?
    When you tested this, what performance differences did you see?

  15. #15
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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. :-)

    Quote Originally Posted by Arjay
    When you tested this, what performance differences did you see?

    regards,
    George

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured