[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-01-22
4
Medium Priority
?
497 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 1000 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 1000 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 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