Link to home
Start Free TrialLog in
Avatar of AndyC1000
AndyC1000

asked on

How to serialise nested objects to XML

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
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial