Solved

How to deserialize xml file with NetDataContractSerializer

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

21 Experts available now in Live!

Get 1:1 Help Now