Solved

How to deserialize xml file with NetDataContractSerializer

Posted on 2012-04-10
1
1,031 Views
Last Modified: 2012-06-21
Hello all,

 How do I deserialize the FileStream?

The code below is how the xml file is created.  Thanks to a solution to a question I posted yesterday.

class Program
    {
        static void Main(string[] args)
        {
            var configuration = new FileConfiguration();

            configuration.AddFile("some", "test", "1.0");
            configuration.AddFile("other", "test2", "2.0");

            var fStream = new FileStream(Directory.GetCurrentDirectory() + @"\test.xml", FileMode.OpenOrCreate);

            var serializer = new NetDataContractSerializer();
            serializer.Serialize(fStream, configuration);

            fStream.Close();


            Console.Read();

        }

        
    }

    [DataContract]
    class FileConfiguration
    {
        private readonly IList<File> _lstFiles;

        public FileConfiguration()
        {
            _lstFiles = new List<File>();
        }

        public void AddFile(string name, string value, string version)
        {
            _lstFiles.Add(new File(name, version, value));
        }

        [DataMember]
        public File[] Files
        {
            get { return _lstFiles.ToArray(); }
        }
    }

    [DataContract]
    class File
    {
        [DataMember]
        public string FileName { get; private set; }

        [DataMember]
        public string FileVersion { get; private set; }

        [DataMember]
        public string FileValue { get; private set; }

        public File(string name, string version, string value)
        {
            FileName = name;
            FileVersion = version;
            FileValue = value;
        }
    }

Open in new window


I've tried the below code block but it returns an ArgumentNull Exception, _lstFiles is null.

var configuration = new FileConfiguration();

            var fStream = new FileStream("C:\\Config.xml", FileMode.Open);

            var serializer = new NetDataContractSerializer();
            serializer.Deserialize(fStream);

            foreach (File f in configuration.Files)
            {
                Console.WriteLine(f.FileValue);
            }

            fStream.Close();

Open in new window


Thanks
0
Comment
Question by:AndyC1000
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
Comment Utility
Here is your code, added setter to Files Property so that Array Can Be Converted To List Again:-

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var configuration = new FileConfiguration();

                configuration.AddFile("some", "test", "1.0");
                configuration.AddFile("other", "test2", "2.0");

                var fStream = new FileStream(Directory.GetCurrentDirectory() + @"\test.xml", FileMode.OpenOrCreate);

                var serializer = new NetDataContractSerializer();
                serializer.Serialize(fStream, configuration);

                fStream.Close();

                fStream = System.IO.File.OpenRead(Directory.GetCurrentDirectory() + @"\test.xml");
                configuration = serializer.Deserialize(fStream) as FileConfiguration;
                fStream.Close();

                if (configuration != null) Console.WriteLine(configuration.Files.Count());
            }
            catch (Exception)
            {
                throw;
            }
            

            Console.Read();

        }

        
    }

    [DataContract]
    public class FileConfiguration
    {
        private IList<File> _lstFiles;

        public FileConfiguration()
        {
            _lstFiles = new List<File>();
        }

        public void AddFile(string name, string value, string version)
        {
            _lstFiles.Add(new File(name, version, value));
        }

        [DataMember]
        public File[] Files
        {
            get { return _lstFiles.ToArray(); }
            private set { _lstFiles = value.ToList(); }
        }

    }

    [DataContract]
    public class File
    {
        [DataMember]
        public string FileName { get; private set; }

        [DataMember]
        public string FileVersion { get; private set; }

        [DataMember]
        public string FileValue { get; private set; }

        public File(string name, string version, string value)
        {
            FileName = name;
            FileVersion = version;
            FileValue = value;
        }
    }

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 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

6 Experts available now in Live!

Get 1:1 Help Now