Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Generic Class inheritance
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 5.0
OS:
Windows 7
Network:
SAMBA Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01611057
Message ID:
01611073
Vues:
51
>Calling save would cause an exception Viv but aside from that I will have to think of something else to achieve what I'm after, I just came across this code on code project and thought I'd give it a shot, it's OK but very rigid in its design , what I'm after is a file ( doesn't have to be app..config ) containing values that are grouped in sections ( like the old INI files ) I suppose I can use plain old XML files or indeed INI files.

If you use the build in configuration manager you can define your own custom sections - but it's pretty rigid (and *.config files aren't necessarily the best place).
Or, as you mention, you could use standalone XML files using the System.Xml.Serialization classes.

A third option is storing as json objects. I like this one because it's compact and can easily handle high levels of complexity. Also, if your using it for a web application there's always the option of passing the json directly to the browser.

The NewtonSoft serializer is ideal for this. Crude example - given the following classes:
    public class Destinations
    {
        public List<Data> data = new List<Data>();
    }

    public class Data
    {
        public string Destination { get; set; }
        public string DialCode { get; set; }
    }
you could:
string path = @"D:\tmp3\myconfig.json";

            Destinations ds = new Destinations();

            ds.data.Add(new Data { Destination = "Hereford", DialCode = "91432" });
            ds.data.Add(new Data { Destination = "Hay", DialCode = "01497" });

            //Save
            var j = JsonConvert.SerializeObject(ds);
            System.IO.File.WriteAllText(path,j);

            //Retrieve and add
            var j2 = System.IO.File.ReadAllText(path);
            Destinations ds2 = JsonConvert.DeserializeObject<Destinations>(j2);
            ds2.data.Add(new Data{Destination = "Monmouth",DialCode = "0600"});
            System.IO.File.WriteAllText(path,JsonConvert.SerializeObject(ds2));
There's a lot more stuff in the NewtonSoft package including streaming and even a 'Linq to Json'. It's available as a NuGet package....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform