XML : C# - Array of a class that holds arrays of another class, readable/writable in XML

I know I'm probably jumping into the deep end with this as I'm kinda new to c#.

My main goal is to create a class (lets call it "test1") with variables, one of which is an array. Another class (we'll call this "test2") will hold other variables, some of which are arrays of the class "test1".

The size of the arrays are variable and not exactly fixed. I want to be able to increase/decrease the size of them during runtime. I also intend for it to be writable and readable to a file, such as XML. So, from what I understand, it has to be capable of being "serialized".

Would it look something like this?

  public class test1  {      public String test1_name;      public String test1_id;      public Single test1_value;      public String[] test1_list;  }    public class test2  {      public test1[] test2_list1;      public test1[] test2_list2;      public String[] test2_list;  }    

I have managed to figure out how to use XML serialization to write the file. E.G.

  System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(test_a)); //test_a being a simple class holding a simple variable    var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//tester.txt";  System.IO.FileStream file = System.IO.File.Create(path);  writer.Serialize(file, tester);  file.Close();    

I want to ensure that I can write and read this data (either as XML or binary, I will decide later) and have it modifiable during runtime, along with the array sizes being adjustable.

Am I going about this the wrong way? How would I construct this and have it usable and saveable/readable?

Can somebody help put me on the right track for what I want to achieve? I know it may be a big ask, but some examples would be so greatly appreciated.

Sorry if this is a bit of a mess.

Thank you all very much in advance!

No comments:

Post a Comment