Click here to Skip to main content
15,889,315 members
Home / Discussions / C#
   

C#

 
AnswerRe: ArrayList stored in Application Settings Pin
DaveyM6920-Mar-09 11:44
professionalDaveyM6920-Mar-09 11:44 
GeneralRe: ArrayList stored in Application Settings [modified] Pin
DaDennis1220-Mar-09 11:57
DaDennis1220-Mar-09 11:57 
GeneralRe: ArrayList stored in Application Settings Pin
DaveyM6920-Mar-09 12:33
professionalDaveyM6920-Mar-09 12:33 
GeneralRe: ArrayList stored in Application Settings Pin
DaDennis1220-Mar-09 12:38
DaDennis1220-Mar-09 12:38 
GeneralRe: ArrayList stored in Application Settings Pin
DaveyM6920-Mar-09 12:51
professionalDaveyM6920-Mar-09 12:51 
AnswerRe: ArrayList stored in Application Settings Pin
DaDennis1220-Mar-09 12:56
DaDennis1220-Mar-09 12:56 
GeneralRe: ArrayList stored in Application Settings Pin
DaveyM6920-Mar-09 13:10
professionalDaveyM6920-Mar-09 13:10 
GeneralRe: ArrayList stored in Application Settings [modified] Pin
DaveyM6920-Mar-09 14:49
professionalDaveyM6920-Mar-09 14:49 
There's no way this should be such a pain! The code below demos how to serialize a generic list of a Serializable class to an XML string, and then recreates the list from the string.

I've kept the string in memory here, but obviously this can be read from / written to the settings. I haven't done any error checking etc so it's not complete, but should be enough get started!
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Group group1 = new Group("Test1", true);
            Group group2 = new Group("Test2", false);
            GroupCollection groups = new GroupCollection();
            groups.AddRange(new Group[] { group1, group2 });

            string xmlString = groups.SerializeToXMLString();

            groups.Clear();
            Console.WriteLine(xmlString);

            groups = GroupCollection.DeSerializeFromXMLString(xmlString);

            foreach (Group group in groups)
            {
                Console.WriteLine(
                    String.Format(
                    "Name={0}, IsVisible={1}",
                    group.Name, group.IsVisible));
            }
        }
    }
    [Serializable]
    public class Group
    {
        public Group()
        {
            Name = string.Empty;
            IsVisible = false;
        }
        public Group(string name)    
        {        
            Name = name;
            IsVisible = false;
        }

        public Group(string name, bool isVisible)
        {
            Name = name;
            IsVisible = isVisible;
        }
        public string Name { get; set; }
        public bool IsVisible { get; set; }
    }
    public class GroupCollection : List<Group>
    {
        public string SerializeToXMLString()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(GroupCollection));
            string xmlString = string.Empty;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                xmlSerializer.Serialize(memoryStream, this);
                xmlString = FromByteArray(memoryStream.GetBuffer());
            }
            return xmlString;
        }

        public static GroupCollection DeSerializeFromXMLString(string xmlString)
        {
            GroupCollection result;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(GroupCollection));
            using (MemoryStream memoryStream = new MemoryStream(ToByteArray(xmlString)))
            {
                result = (GroupCollection)xmlSerializer.Deserialize(memoryStream);
            }
            return result;
        }
        private String FromByteArray(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            string xmlString = encoding.GetString(characters);
            return (xmlString);
        }
        private static Byte[] ToByteArray(string xmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(xmlString);
            return byteArray;
        }
    }
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

modified on Friday, March 20, 2009 9:12 PM

GeneralRe: ArrayList stored in Application Settings Pin
DaveyM6920-Mar-09 15:53
professionalDaveyM6920-Mar-09 15:53 
GeneralRe: ArrayList stored in Application Settings Pin
DaveyM6921-Mar-09 9:35
professionalDaveyM6921-Mar-09 9:35 
GeneralRe: ArrayList stored in Application Settings Pin
DaDennis1222-Mar-09 3:47
DaDennis1222-Mar-09 3:47 
GeneralRe: ArrayList stored in Application Settings Pin
DaveyM6922-Mar-09 4:34
professionalDaveyM6922-Mar-09 4:34 
QuestionDézipe file with c# Pin
abbd20-Mar-09 11:09
abbd20-Mar-09 11:09 
AnswerRe: Dézipe file with c# Pin
Mbah Dhaim20-Mar-09 11:15
Mbah Dhaim20-Mar-09 11:15 
GeneralRe: Dézipe file with c# Pin
abbd21-Mar-09 0:08
abbd21-Mar-09 0:08 
AnswerRe: Dézipe file with c# Pin
Ravadre20-Mar-09 12:01
Ravadre20-Mar-09 12:01 
QuestionShow a console window for 10sec in C# console application Pin
abbd20-Mar-09 11:00
abbd20-Mar-09 11:00 
AnswerRe: Show a console window for 10sec in C# console application Pin
Mbah Dhaim20-Mar-09 11:14
Mbah Dhaim20-Mar-09 11:14 
GeneralRe: Show a console window for 10sec in C# console application Pin
abbd20-Mar-09 11:49
abbd20-Mar-09 11:49 
GeneralRe: Show a console window for 10sec in C# console application Pin
Mbah Dhaim20-Mar-09 12:55
Mbah Dhaim20-Mar-09 12:55 
AnswerRe: Show a console window for 10sec in C# console application Pin
Ravadre20-Mar-09 16:40
Ravadre20-Mar-09 16:40 
QuestionVerify if a processes exist Pin
abbd20-Mar-09 9:59
abbd20-Mar-09 9:59 
QuestionHow do I cast a LPARAM? Pin
bbranded20-Mar-09 8:41
bbranded20-Mar-09 8:41 
QuestionRe: How do I cast a LPARAM? Pin
led mike20-Mar-09 8:53
led mike20-Mar-09 8:53 
AnswerRe: How do I cast a LPARAM? [modified] Pin
bbranded20-Mar-09 8:56
bbranded20-Mar-09 8:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.