C#, deserialize LIST<T> object to class, is there better solution. Why

Below take the XML file which contain configuration data for the project and deserialize.
(I have written data which worked well via serialize, so it not shown here)

the code also copy the LIST<T> object back to class object which is (public) accessible by other class instance.

I'm wonder if there better way to do this, especially copy List<T> object back to class which is easier to access.

  
            List<ConfigurationX> config;
            ConfigData = new ConfigurationX();

            if (File.Exists(filePath))
            {
                // Deserialize the existing list or create an empty one if none exists yet.
                config = DeserializeFromFile<List<ConfigurationX>>(filePath);
                ConfigData.CompanyID = config[0].CompanyID;
                ConfigData.CompanyString = config[0].CompanyString;
                ConfigData.Test1 = config[0].Test1;
                ConfigData.Test2 = config[0].Test2;
                ConfigData.Test3 = config[0].Test3;
            }

....
        public static T DeserializeFromFile<T>(string xmlFileName) where T : class
        {
            using (FileStream stream = new FileStream(xmlFileName, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                return serializer.Deserialize(stream) as T;
            }
        }
.....

Open in new window


The class look like this

[Serializable]
        public class ConfigurationX
        {
        // This is new style Getter and Setter for modern .NET typestyle. 
        // ==================================================================Variable as part of XML Configuration Data


        // ==================================================================Getter/Setter
        public uint CompanyID { get; set; }
        public string CompanyString { get; set; }
        public uint Test1 { get; set; }
        public uint Test2 { get; set; }
        public uint Test3 { get; set; }

        // ==================================================================constructor
        public ConfigurationX()
        {
            // Leave blank, do not install default data here.
        }

        public void ConfigurationXDefault()
        {
            CompanyID = 10;
            CompanyString = "ZMDI";
            Test1 = 9678;
            Test2 = 34453;
            Test3 = 12981;
        }
    }

Open in new window


I'm still on learning curve with List<> generic object

I'm open for better solution or methodology to establish if I'm doing right method or not.
Richard PayneChief Technology EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
First you need to realize that if you properly serialize and deserialize an object, the deserialized object *is* the object.  So you don't have to create a new ConfigurationX object just to set the properties.

Consider the following -
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace EE_Q28740908
{
	class Program
	{
		static List<ConfigurationX> configurations;

		static void Main(string[] args)
		{
			if (File.Exists("Configurations.xml"))
			{
				configurations = DeserializeFromFile<List<ConfigurationX>>("Configurations.xml");
				foreach (var config in configurations)
					Console.WriteLine(config);
			}
			else
			{
				if (configurations == null)
				{
					configurations = new List<ConfigurationX>();
					for (int i = 0; i < 10; i++)
						configurations.Add(new ConfigurationX() { CompanyID = (uint)i, CompanyString = string.Format("ZMDI{0}", i) });
				}
				SerializeToFile<List<ConfigurationX>>("Configurations.xml");
				Console.WriteLine("Created configurations file.");
			}
			Console.ReadLine();
		}

		static T DeserializeFromFile<T>(string fileName) where T : class
		{
			if (typeof(T).IsSerializable)
			{
				using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
				{
					XmlSerializer serializer = new XmlSerializer(typeof(T));
					return (T)serializer.Deserialize(stream);
				}
			}
			return default(T);
		}

		static void SerializeToFile<T>(string fileName) where T : class
		{
			if (typeof(T).IsSerializable)
			{
				using (var stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite))
				{
					XmlSerializer serializer = new XmlSerializer(typeof(T));
					serializer.Serialize(stream, configurations);
				}
			}
		}
	}

	[Serializable]
	public class ConfigurationX
	{
		public uint CompanyID { get; set; }
		public string CompanyString { get; set; }
		public uint Test1 { get; set; }
		public uint Test2 { get; set; }
		public uint Test3 { get; set; }

		public ConfigurationX()
		{
			// Leave blank, do not install default data here.
		}

		public void ConfigurationXDefault()
		{
			CompanyID = 10;
			CompanyString = "ZMDI";
			Test1 = 9678;
			Test2 = 34453;
			Test3 = 12981;
		}

		public override string ToString()
		{
			return string.Format("ID: {0}; String: {1}; Test1: {2}; Test2: {3}; Test4: {4}", CompanyID, CompanyString, Test1, Test2, Test3);
		}
	}
}

Open in new window

The first run yields -Capture.JPGWith the following Xml contents -
<?xml version="1.0"?>
<ArrayOfConfigurationX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ConfigurationX>
    <CompanyID>0</CompanyID>
    <CompanyString>ZMDI0</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>1</CompanyID>
    <CompanyString>ZMDI1</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>2</CompanyID>
    <CompanyString>ZMDI2</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>3</CompanyID>
    <CompanyString>ZMDI3</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>4</CompanyID>
    <CompanyString>ZMDI4</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>5</CompanyID>
    <CompanyString>ZMDI5</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>6</CompanyID>
    <CompanyString>ZMDI6</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>7</CompanyID>
    <CompanyString>ZMDI7</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>8</CompanyID>
    <CompanyString>ZMDI8</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
  <ConfigurationX>
    <CompanyID>9</CompanyID>
    <CompanyString>ZMDI9</CompanyString>
    <Test1>0</Test1>
    <Test2>0</Test2>
    <Test3>0</Test3>
  </ConfigurationX>
</ArrayOfConfigurationX>

Open in new window

If I run the program again; I get the following results -Capture.JPGWhat this shows is that I am dealing with my list of configuration objects.  From there if I need a specific object I can access it in a multitude of ways; e.g. - Linq, For loop, For Each loop, While loop, index access (as you did), etc. etc...

-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard PayneChief Technology EngineerAuthor Commented:
Well done!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.