1

我正在为我的一个客户编写一种配置存储方法,他们要求它采用 XML 格式。除了一个问题之外,我已经设法让它发挥作用;缺少第一个元素。我的 XML 文件是:

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

我的解析命令是:

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

结果(configstr)是:

autologin = true
config = 
password = pass
4

1 回答 1

2
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");
于 2012-11-11T01:36:54.200 回答