JohnHoulderUK
JohnHoulderUK

Reputation: 679

XMLTextReader missing first element

I'm writing a configuration storage method for one of my clients and they requested that it be in XML. I've managed to get it to work other than one issue; the first element is missing. My XML file is:

<?xml version="1.0" encoding="utf-8" ?>
<config>
    <username>test</username> 
    <password>pass</password>
    <autologin>true</autologin>
</config>

My Parsing command is:

    void parseConfigFile()
    {
        while (configstr.Read())
        {
            if (configstr.IsStartElement())
            {
                config.Add(configstr.Name,configstr.ReadString());
            }
        }
    }

and the result (configstr) is:

autologin = true
config = 
password = pass

Upvotes: 1

Views: 181

Answers (1)

dtb
dtb

Reputation: 217253

var document = XDocument.Load("file.xml");
var config = document.Root;

var userName = (string)config.Element("username");
var password = (string)config.Element("password");
var autologin = (bool)config.Element("autologin");

Upvotes: 2

Related Questions