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
Solved

How to deserialize xml file with NetDataContractSerializer

Posted on 2012-04-10
1
1,067 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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