Click here to Skip to main content
15,917,538 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to draw onto a WebBrowser form Pin
sfdsaadaadadad26-Jun-09 5:41
sfdsaadaadadad26-Jun-09 5:41 
QuestionWay to monitor API calls of programs? Pin
bbranded20-Mar-09 12:42
bbranded20-Mar-09 12:42 
AnswerRe: Way to monitor API calls of programs? Pin
Garth J Lancaster20-Mar-09 13:40
professionalGarth J Lancaster20-Mar-09 13:40 
GeneralRe: Way to monitor API calls of programs? [modified] Pin
bbranded20-Mar-09 14:11
bbranded20-Mar-09 14:11 
GeneralRe: Way to monitor API calls of programs? Pin
Garth J Lancaster20-Mar-09 14:23
professionalGarth J Lancaster20-Mar-09 14:23 
GeneralRe: Way to monitor API calls of programs? Pin
bbranded20-Mar-09 14:37
bbranded20-Mar-09 14:37 
GeneralRe: Way to monitor API calls of programs? Pin
Garth J Lancaster20-Mar-09 14:40
professionalGarth J Lancaster20-Mar-09 14:40 
QuestionHL7 Pin
M.Ambigai20-Mar-09 11:48
M.Ambigai20-Mar-09 11:48 
AnswerRe: HL7 Pin
Garth J Lancaster20-Mar-09 12:36
professionalGarth J Lancaster20-Mar-09 12:36 
AnswerRe: HL7 Pin
#realJSOP21-Mar-09 3:35
professional#realJSOP21-Mar-09 3:35 
QuestionArrayList stored in Application Settings Pin
DaDennis1220-Mar-09 11:34
DaDennis1220-Mar-09 11:34 
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 

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.