Hi everyone,

I'm new to c# ( I've done java before) and I'm having a problem with one of our tutorials. We have to read some data from an XML file. When doing it with a console application it turned out fine, but when doing it with a Windows forms application I tried to load the information to a label. Now the problem is it only shows the data of last element(book) in the XML file, all the others are getting written over I think. So how can I stop this from happening and display the whole XML file in the text box.
Many thanx.

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;
using System.IO;

namespace Tute3
{
    public partial class Form2 : Form
    {
        String a, b;
        string workingDir = Directory.GetCurrentDirectory();
        
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            XmlTextReader textReader = new XmlTextReader(workingDir + @"\books.xml");
            while (textReader.Read())
            {
                textReader.MoveToElement();

                if (textReader.Name == "title")
                {
                    textReader.Read();
                    XmlNodeType nType = textReader.NodeType;
                    if (nType == XmlNodeType.Text)
                    {
                      a=("Title:" + textReader.Value.ToString());
                    }
                }

                if (textReader.Name == "author")
                {
                    textReader.Read();
                    XmlNodeType nType = textReader.NodeType;
                    if (nType == XmlNodeType.Text)
                    {
                        b=("Name:" + textReader.Value.ToString());
                    }
                }
       
            //Writes the data to the label
            this.label1.Text = a + "\n" + b ;
            }
            textReader.Close();
        }
    }
}

Recommended Answers

All 4 Replies

The bad way is to change line 37 to read a += ... (same with line 47, change it to b += ... . The good way is to learn to use StringBuilder

The bad way is to change line 37 to read a += ... (same with line 47, change it to b += ... . The good way is to learn to use StringBuilder

Hey Momerath,
Thanx a lot for the trouble. The += thing worked, sort of. Now it reads as;
title1, title2
author1,author2
Is there a way to make it read like;
title1,author1
title2,author2.

And what exactly can I use in the StringBuilder class to make it work in the good way...??

Another "bad" thing is to use short variable names like a or b. Call them TitleStr or AuthorStr or something more meaningfull.
Short variable names may seem harmless here, they will kill you in code of a 1000 and more lines!
On line 52 change a + "\n" + b in a + b + "\n"
With a StringBuilder called MyStr this would become

MyStr.Append(a);
MyStr.AppendLine(b);

Thanx guyz, it worked and I think I got the hang of the Append method. And yeah ddanbe we were told not to use meaningless variables but I plain forgot about that when writing the code...any way thanx a lot, both of you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.