Hi Guys, i have parsed an xml file below and want to create another class with data structure and pass these values to those data structure.

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
using System.Diagnostics;
namespace ScormTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Array[] itemcoll = null;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                string xmlFilename = op.FileName;
                //XmlTextReader reader = new XmlTextReader(xmlFilename);
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(xmlFilename);
                XmlNodeList title = xDoc.GetElementsByTagName("title");
                MessageBox.Show("Title" + title[0].InnerText);
                XmlNodeList item = xDoc.GetElementsByTagName("item");
                for (int i = 0; i < item.Count; i++)
                {
                    MessageBox.Show("Item" + item[i].InnerText);
                    
                }
               XmlNodeList fl = xDoc.GetElementsByTagName("file");
                for (int j = 0; j < fl.Count; j++)
                {
                    XmlAttributeCollection att = fl[j].Attributes;
                    MessageBox.Show(att[0].Value);
                }

            }

what i want to do is to create another class and create datastructure and then assign the value of nodelist and attribute to those data stucture.Can anyone help me with this.

Thanks

if you need to assign just those two values in the data structure, you may want to try Dictionary class.

Dictionary<string, string> list = new Dictionary<string, string>();

no it wont be just two .It will lot more than two.I am using visual studio and whta i want is to attach another class to my project and create data structure in that class.

You can use either a class or struct. The syntax is very similar; structs have a smaller footprint i believe, and are used where the data is immutable.
Create your class/struct, add variables to store the different data values, then use properties to access them.
You can then store each instance of your class in a typed list:

publuc partial class Form1 : Form
    {
        List<Employee> Employees = new List<Employee>();

        private void StoreEmployees()
        {
            Employee emp = new Employee();
            emp.Name = "John Smith";
            emp.DOB = DateTime.Now; //recruit them young! :p
            emp.Department = "Nursery";

            Employees.Add(emp);

            MessageBox.Show(string.Format("{0} works in {2}", Employees[0].Name, Employees[0].Department), "Employee 1");
        }

    }

    public struct Employee
    {
        public string Name { get; set; }
        public DateTime DOB { get; set; }
        public string Department { get; set; }
    }

You can do the same thing dynamically using a DataTable.

DataTable Employees = null;

        private void BuildTable()
        {
            Employees = new DataTable();
            Employees.Columns.Add("Name", typeof(string));
            Employees.Columns.Add("DOB", typeof(DateTime));
            Employees.Columns.Add("Department", typeof(string));
        }

        private void StoreEmployees()
        {
            DataRow row = Employees.NewRow();
            row["Name"] = "John Smith";
            row["DOB"] = DateTime.Now; //recruit them young! :p
            row["Department"] = "Nursery";

            Employees.Rows.Add(row);

            MessageBox.Show(string.Format("{0} works in {1}", 
                             Employees.Rows[0]["Name"], 
                             Employees.Rows[0]["Department"]), "Employee 1");
        }

Whether you use a generic list or a datatable depends mostly on what you wish to do with the data.
If you are going to display it in a datagridview, or save it to a database, then the datatable is easier ot work with. The DataTable class also has a .ReadXML() method that may be useful to you.
If you are going to iterate through the data then the list is more efficient.

I'd suggest reading up on both classes and deciding for yourself which best fits your needs.

This article has been dead for over six months. Start a new discussion instead.