Solved

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

Posted on 2013-01-22
4
454 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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now