c# - Add array to array of keys in web.config -
c# - Add array to array of keys in web.config -
my web.config looks this
<add key="studentname1" value="english, maths, french" /> <add key="studentname2" value="english" /> <add key="studentname3" value="english, french" />
the number of students can increased or decreased. how read in c#? array within array
since want store array/list of students should serializing. create models this:
public class pupil { public string name { get; set;} public list<course> courses { get; set; } } public class course of study { public string name { get; set;} } public class mysettings { public list<student> students {get; set;} }
you can add together students students list within mysettings. each pupil have 0 or more courses (rather doing comma separated variant).
then serialize mysettings , store on filesystem.
public static class myserializer { public static t toobject<t>(string path, string filename) { string fullpath = path + filename; if (!path.endswith("/")) { fullpath = path + "/" + filename; } xmlserializer serializer = new xmlserializer(typeof(t)); using (streamreader filereader = new streamreader(fullpath)) { t instance = (t)serializer.deserialize(filereader); homecoming instance; } } public static void tofile<t>(t currentobject, string path, string filename) { string fullpath = path + filename; if (!path.endswith("/")) { fullpath = path + "/" + filename; } xmlserializer serializer = new xmlserializer(typeof(t)); using (system.io.streamwriter file = new system.io.streamwriter(fullpath)) { serializer.serialize(file, currentobject); } } }
you can utilize solution this:
var pupil = new student(); student.name = "student1"; student.courses = new list<course>() { new course() { name = "english" } }; mysettings settings = new mysettings(); settings.students = new list<student>(); settings.students.add(student); myserializer.tofile<mysettings>(settings, @"c:\temp", "settings.xml"); // serialize c:\temp\settings.xml settings = myserializer.toobject<mysettings>(@"c:\temp", "settings.xml"); // deserialize c:\temp\settings.xml
c# web-config
Comments
Post a Comment