Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7412679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:34:47+00:00 2026-05-29T06:34:47+00:00

This loads a set of values from an XML file and places them into

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:34:48+00:00Added an answer on May 29, 2026 at 6:34 am

    Are the members of your class fields or properties? Either way, a little reflection and Linq should allow you to enumerate through the data members of your class, after you have hydrated an instance of it from your XML file.

    var fieldDictionary = 
       (from f in typeof(UnitAttributes).GetFields()
        select new {Name = f.Name, Value = (string)(f.GetValue(attributes))})
       .ToDictionary(x=>x.Name, x=>x.Value);
    

    fieldDictionary is now a Dictionary<string, string> (which is an IEnumerable<KeyValuePair<string, string>>), which should be suitable for loading into a ListBox.

    Be advised; reflection is slow. It would be far more preferable for you to modify or extend your UnitAttributes class to implement IEnumerable (probably of a Tuple, maybe a KeyValuePair). It would also allow you to enumerate the properties of an instance of the class in exactly the order you want, instead of the order in which they’re defined, or by some other FieldInfo/PropertyInfo data like the field’s name.

    Also be aware that a field is not a property, and vice versa. If you have a mix of properties and public fields on your class, I would HIGHLY recommend standardizing to one or the other; otherwise you’ll have to reflect BOTH a list of properties and a list of fields using two of the above Linq statements (at a cost of roughly double the run time), and there will be no chance of them being in any custom-defined order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract values from an XML file using linq to create
I am trying to load/show completely different set of values in a combobox(this one
Update: Turns out this is undocumented behaviour of the NetStream class - NetStream loads
Has anyone ever run into this before? I have the crossdomain.xml set and I've
I'm writing out some xml from C# using the .net framework's XmlTextWriter. This works
Hi I am trying to read XML File using XLinq and binding the values
I wan't to load some data from my XML file using this function: public
I' trying to write an xml file from Excel VBA using Microsoft XML 6.0.
i was trying to figure out what is wrong with this piece of code,
I'm parsing multiple audio file uri's from XML and binding them to MediaElement's Source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.