0

As a beginner at c#, i am running against a wall with the following:

Basically, the object of this is to try to read information about classes-formatted in xml, into a table (listbox)... This is not happenning.

So, Im trying to read xml and append string builder to print to listbox per line.

instead the same statement is pritned on 1713 lines. Here is the code:

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.Xml;

namespace StartProgram
{
    public partial class ModuleSummary : Form
    {
        public ModuleSummary()
        {
            InitializeComponent();
            XmlTextReader moduleReader = new XmlTextReader("c:\\users\\w1283057\\documents\\visual studio 2010\\Projects\\StartProgram\\StartProgram\\myCourses.xml");
            //to revise...

            System.Text.StringBuilder moduleEntry = new System.Text.StringBuilder();
            while(moduleReader.Read())
            {
                if ((moduleReader.NodeType == XmlNodeType.Element) && (moduleReader.Name == "moduleCode"))
                    {                        
                        moduleEntry.Append(moduleReader.ReadElementContentAsString() + " ");
                    }
                    if ((moduleReader.NodeType == XmlNodeType.Element) && (moduleReader.Name == "moduleTitle"))
                    {
                        moduleEntry.Append(moduleReader.ReadElementContentAsString() + " ");
                    }
                    if ((moduleReader.NodeType == XmlNodeType.Element) && (moduleReader.Name == "credits"))
                    {
                        moduleEntry.Append(moduleReader.ReadElementContentAsString()+" ");
                    }
                    if ((moduleReader.NodeType == XmlNodeType.Element) && (moduleReader.Name == "level"))
                    {
                        moduleEntry.Append(moduleReader.ReadElementContentAsString() + " ");
                    }
                    if ((moduleReader.NodeType == XmlNodeType.Element) && (moduleReader.Name == "semester"))
                    {
                        moduleEntry.Append(moduleReader.ReadElementContentAsString() + " test ");
                    }
                    moduleSummaryBox.Items.Add(moduleEntry);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            AddModule frm = new AddModule();
            frm.Show();
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72

1 Answers1

2

You need to read your XML in using a XmlDocument and parse it that way. There's plenty of tutorials online.. http://www.functionx.com/csharp2/xml/Lesson02d.htm

banging
  • 2,540
  • 1
  • 22
  • 26
  • thank you for your reply. Im attempting to append several elements per line, and ive been unsucessful doing so – Maciej Cygan May 03 '12 at 20:02
  • 1
    FYI, you also don't want to be doing all of this in the constructor. Pull all but the `InitializeComponent()` call into `Form_Load`. – John Saunders May 03 '12 at 20:06
  • @MaciejCygan I can't visualise what you want anymore. First you say you want "print to listbox per line" and now you say "append several elements per line". Those are contradicting statements. It should help if you give us sample data and illustrate how you want it loaded. – banging May 03 '12 at 20:11
  • @banging Apologies I wasn't clear. What I want to happen is I have an XML as such: ' ECSC401 Programming Methodology 15 1 4 ' What I want to achieve is " ECSC401 Programming Methodology 15 1 4" on a single line within a listBox and then the next module on the line following that and so on... – Maciej Cygan May 03 '12 at 20:14