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();
}
}
}