Problem with XML and C#

Master Summoner
Joined
Jan 16, 2005
Messages
576
Reaction score
0
Hey, I'm using a .xml to save a program's settings. I can create and save the settings to the .xml file but I can't read the data from the file, I always get null values.

Here's the loading code:

Code:
textReader = new XmlTextReader("c:\\settings.xml");
textReader.Read();

while (textReader.Read())
{

    if (textReader.LocalName == "Username")
          lblTest1.Text = textReader.Value;

    if (textReader.LocalName == "Password")
          lblTest2.Text = textReader.Value;

    if (textReader.LocalName == "Adress")
          lblTest3.Text = textReader.Value;

    if (textReader.LocalName == "Database")
          lblTest4.Text = textReader.Value;
}

And here's the contents of Settings.xml:

Code:
<?xml version="1.0"?>
<ROOT>
  <Username>sa</Username>
  <Password>teste</Password>
  <Adress>teste</Adress>
  <Database>Northwind</Database>
</ROOT>
 
well, C# has practically nothing already set to interact with INI files, and I kinda wanted to learn how to work with XML, they're really useful at storing information...
 
yeah made it!!

here's how to do it if some1 has a similar problem:

Code:
XmlDocument document = new XmlDocument();
document.Load("c:\\settings.xml");
XmlElement xmlROOT = document.DocumentElement;
XmlNode xmlSettings = xmlROOT.FirstChild;

XmlNodeList xmllist = xmlSettings.ChildNodes;

foreach (XmlNode node in xmllist)
{
     if (String.Equals(strUsername, node.Name))
         lblTest1.Text = node.InnerText;
     if (String.Equals(strPassword, node.Name))
         lblTest2.Text = node.InnerText;
     if (String.Equals(strAdress, node.Name))
         lblTest3.Text = node.InnerText;
     if (String.Equals(strDatabase, node.Name))
         lblTest4.Text = node.InnerText;
}
 
Back