Solved

How to deserialize xml file with NetDataContractSerializer

Posted on 2012-04-10
1
1,100 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
[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
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37831316
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

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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