?
Solved

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

Posted on 2013-01-22
4
Medium Priority
?
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

801 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