1 Replies - 75 Views - Last Post: 06 February 2012 - 08:31 PM Rate Topic: -----

Topic Sponsor:

#1 negligible  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 56
  • View blog
  • Posts: 272
  • Joined: 02-December 10

Outputting class of stored values to list

Posted 06 February 2012 - 05:51 PM

Hi there,

This loads a set of values from an XML file and places them into a class for storage. I'm trying to figure out how to output the values as a list so I can place them into a Listbox.

I thought there would be an easy way like a .ToList() method or to be able to foreach through the strings in the class (no public GetEnumerator). I've been able to find out that Foreach hides some of the complexity but not away to do what I want.

I've been searching online with no avail (lacking the correct terminology maybe), unfortunately I left my C# reference books at work :/

Would much appreciate a pointer in the right direction,
Thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                listBox1.Items.Clear();

                string path = "characterXML.xml";
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                System.Xml.XmlDocument CXML = new System.Xml.XmlDocument();

                CXML.Load(fs);

                //Get the number of elements 
                XmlNodeList elemList = CXML.GetElementsByTagName("unit");

                //foreach (var element in elemList)
                //{
                //    listBox1.Items.Add(element);
                //}

                for (int i = 0; i < elemList.Count; i++)
                {
                    UnitAttributes attributes = new UnitAttributes();

                    attributes.army = elemList[i].Attributes["army"].Value;
                    attributes.category = elemList[i].Attributes["category"].Value;
                    attributes.type = elemList[i].Attributes["type"].Value;
                    attributes.composition = elemList[i].Attributes["composition"].Value;
                    attributes.WS = elemList[i].Attributes["WS"].Value;
                    attributes.BS = elemList[i].Attributes["BS"].Value;
                    attributes.T = elemList[i].Attributes["T"].Value;
                    attributes.W = elemList[i].Attributes["W"].Value;
                    attributes.I = elemList[i].Attributes["I"].Value;
                    attributes.A = elemList[i].Attributes["A"].Value;
                    attributes.LD = elemList[i].Attributes["LD"].Value;
                    attributes.save = elemList[i].Attributes["Save"].Value;
                    attributes.armour = elemList[i].Attributes["armour"].Value;
                    attributes.weapons = elemList[i].Attributes["weapons"].Value;
                    attributes.specialrules = elemList[i].Attributes["specialrules"].Value;
                    attributes.transport = elemList[i].Attributes["transport"].Value;
                    attributes.options = elemList[i].Attributes["options"].Value;

                    //foreach (string item in attributes)
                    //{

                        //unit.Add(item);
                    //}
                    //listBox1.Items.AddRange(attributes)

                }  
                	                
                //Close the filestream
                fs.Close();
            }
            catch (Exception ex)
            {

            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ThereIsOnlyRules
{
    class UnitAttributes
    {
        public string army { get; set; }
        public string category { get; set; }
        public string type { get; set; }
        public string composition { get; set; }
        public string WS { get; set; }
        public string BS { get; set; }
        public string T { get; set; }
        public string W { get; set; }
        public string I { get; set; }
        public string A { get; set; }
        public string LD { get; set; }
        public string save { get; set; }
        public string armour { get; set; }
        public string weapons { get; set; }
        public string specialrules { get; set; }
        public string transport { get; set; }
        public string options { get; set; }
    }
}


<?xml version="1.0"?>
<config>
<unit
army="Tyranids"
category="Troops"
type="Infantry"
composition="10-30"
WS="3"
BS="3"
T="3"
W="1"
I="4"
A="1"
LD="6"
Save="6+"
armour="Chitin"
weapons="Claws and Teeth, Fleshborer"
specialrules="Instictive Behaviour - Lurk, Move Through Cover"
transport="If the brood consists of 20 models or less, it may take a Mycetic Spore."
options="Strangleweb, Spinefists, Spike rifle, Devourer, Adrenal Glands, Toxin Sacs"
>
Termagant Brood
</unit>
<unit
army="Tyranids"
category="Troops"
type="Infantry"
composition="10-30"
WS="3"
BS="3"
T="3"
W="1"
I="5"
A="2"
LD="6"
Save="6+"
armour="Chitin"
weapons="Scything Talons"
specialrules="Instictive Behaviour - Feed, Bounding Leap, Fleet, Move Through Cover"
transport="If the brood consists of 20 models or less, it may take a Mycetic Spore."
options="Adrenal Glands, Toxin Sacs"
>
Hormagaunt Brood
</unit>
</config>


Is This A Good Question/Topic? 0
  • +

Replies To: Outputting class of stored values to list

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3239
  • View blog
  • Posts: 10,644
  • Joined: 18-April 07

Re: Outputting class of stored values to list

Posted 06 February 2012 - 08:31 PM

Well one thing you can do is use reflection to get the properties of your class and iterate through them. Something like this may work for you...

PropertyInfo[] unitAtts = typeof(UnitAttributes).GetProperties(BindingFlags.Public|BindingFlags.Instance);

foreach (PropertyInfo propInfo in unitAtts) {
      String attrib = (String)propInfo.GetValue(<class instance goes here>, null);
      MessageBox.Show(attrib);
}



What this is going to do is iterate through the public properties of one of your classes and show its value. Where I have marked it as "class instance goes here" you will put an instance of your UnitAttributes object.

Thus this code will start looping through army, category, type... etc and display their values.

Hope this is close enough to what you are looking for. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1