Solved

How do I serialize a class using the XMLSerializer in .Net 4.0 using C#?

Posted on 2013-01-22
4
468 Views
Last Modified: 2013-01-25
Hi:

I'm trying to serialize a class using the XMLSerializer.
I get a runtime exception when I attempt to instantiate an instance of the serializer.
What am I missing/doing wrong here?



// Definition of Template File Class

 [Serializable()]
    public class TemplateFile
    {
        /// <summary>
        ///
        /// </summary>
        List<String> lCaseNumbers = new List<String>();
        public List<String> CaseNumbers
        {
            get { return lCaseNumbers; }
            set { lCaseNumbers = value; }
        }
    }

 private void SaveReportTemplate()
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(TemplateFile));
                StringWriter writer = new StringWriter();
                String xmlData;

                // Now serialize
                TemplateFile templateFile = new TemplateFile();
                templateFile.CaseNumbers = lCaseNumbers;

                xmlSerializer.Serialize(writer, lCaseNumbers);
                writer.Close();
                xmlData = writer.ToString();
            }
        }



I get the following runtime exception when I attempt to create a new instance of a serializer based on my TemplateFile class.
========================================================================
The assembly with display name 'Snappy.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'Snappy.XmlSerializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

What am I doing wrong?
(Snappy is the name of the exe I'm building)

Thanks,
JohnB
0
Comment
Question by:jxbma
  • 2
4 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 38808511
In this line:

xmlSerializer.Serialize(writer, lCaseNumbers);

Open in new window


...you are attempting to serialize the list itself. However, in this line:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(TemplateFile));

Open in new window


...you told the XmlSerializer that you would be passing a TemplateFile object. Change the Serialize call to this instead:

xmlSerializer.Serialize(writer, templateFile);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38808514
P.S.

For XML serialization, the [Serializable] attribute is not required--but it doesn't hurt anything to leave it in.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 38808654
This is an expected exception, as far as I know, and won't stop your application in normal execution. Just ignore the exception.
0
 
LVL 4

Expert Comment

by:jiangsheng
ID: 38811694
There is a performance hit (memory fragmentation and runtime compiling) to let the runtime to generate one xml serialize assembly per type.

You can use Sgen.exe or integrate it as part of build process to generate the xml serializers and ship the xml serializer assemblies along with your application.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 55
Build a string of emails from a gridview 2 17
C# LINQ 5 25
Convert VB web project to C# 3 38
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question