Solved

How to deserialize xml file with NetDataContractSerializer

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 56
Angular JS Route 3 69
System.Security 2 27
insert value of checklistbox checked 4 25
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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