Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to serialise nested objects to XML

Posted on 2012-04-10
1
Medium Priority
?
389 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

782 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