Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

609 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