Problem with XML and C#

Results 1 to 4 of 4
  1. #1
    Grand Master MuRdOc is offline
    Grand MasterRank
    Jan 2005 Join Date
    PortugalLocation
    836Posts

    Problem with XML and C#

    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>


  2. #2
    Grand Master username1 is offline
    Grand MasterRank
    Jul 2004 Join Date
    5,867Posts
    Why don't you use .INI files? Much simpler, especially if you're only storing name=value pairs.

  3. #3
    Grand Master MuRdOc is offline
    Grand MasterRank
    Jan 2005 Join Date
    PortugalLocation
    836Posts
    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...

  4. #4
    Grand Master MuRdOc is offline
    Grand MasterRank
    Jan 2005 Join Date
    PortugalLocation
    836Posts
    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;
    }



Advertisement