Solved

How to serialise nested objects to XML

Posted on 2012-04-10
1
362 Views
Last Modified: 2012-04-10
Hello,

I'm having troubles serializing nested objects.

Currently I have :
<?xml version="1.0" encoding="utf-8"?>
<FileConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Version>1.0</Version>
  <FileType>abc</FileType>
  <FileValue>abc.txt</FileValue>
</FileConfiguration><?xml version="1.0" encoding="utf-8"?>
<FileConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Version>1.0</Version>
  <FileType>abc1</FileType>
  <FileValue>abc1.txt.txt</FileValue>
</FileConfiguration>

Open in new window


But need:
<?xml version="1.0" encoding="utf-8"?>
<FileConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Files>
  <Version>1.0</Version>
  <FileType>abc</FileType>
  <FileValue>abc.txt</FileValue>
 </Files>
<Files>
  <Version>1.0</Version>
  <FileType>abc1</FileType>
  <FileValue>abc1.txt.txt</FileValue>
</Files>
</FileConfiguration>

Open in new window


Here is the code, I 've tried to nest a File object in FileConfiguration it turned into a mess.  Please provide some examples.  I'm not sure how to deserialize method will change as well.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConfigurationFileCreator
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {

            FileConfiguration c = new FileConfiguration();
            c.FileType = "abc";
            c.FileValue = "abc.txt";
            c.Version = "1.0";


            FileConfiguration c1 = new FileConfiguration();
            c1.FileType = "Climate";
            c1.FileValue = "Climate.txt";
            c1.Version = "1.0";

            // Serialize the configuration object to a file
            FileConfiguration.Serialize("C:\\Configv1.xml", c, c1);

        }
    }


    #region -- FileConfiguration Class --
   
    [Serializable]
    
    public class FileConfiguration
    {
        string _Version;
        string _FileType;
        string _FileValue;

        public FileConfiguration()
        {
            _Version = "";
            _FileType = "";
            _FileValue = "";
        }



        public static void Serialize(string file, FileConfiguration c, FileConfiguration c1)
        {
            System.Xml.Serialization.XmlSerializer xs
               = new System.Xml.Serialization.XmlSerializer(c.GetType());
            StreamWriter writer = File.CreateText(file);
            xs.Serialize(writer, c);
            xs.Serialize(writer, c1);
            writer.Flush();
            writer.Close();
        }
//        public static Configuration Deserialize(string file)
//        {
//            System.Xml.Serialization.XmlSerializer xs
//               = new System.Xml.Serialization.XmlSerializer(
//                  typeof(Configuration));
//            StreamReader reader = File.OpenText(file);
//            Configuration c = (Configuration)xs.Deserialize(reader);
//            reader.Close();
//            return c;
//        }
        public string Version
        {
            get { return _Version; }
            set { _Version = value; }
        }
        public string FileType
        {
            get { return _FileType; }
            set { _FileType = value; }
        }
        public string FileValue
        {
            get { return _FileValue; }
            set { _FileValue = value; }
        }

    }
    #endregion
}

Open in new window


Thanks
0
Comment
Question by:AndyC1000
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37826541
your class should look like this to acheive what you are trying to acheive:-

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

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

8 Experts available now in Live!

Get 1:1 Help Now