首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#读取Xml配置文件

C#读取Xml配置文件
EN

Stack Overflow用户
提问于 2017-11-18 21:57:39
回答 1查看 3.6K关注 0票数 0

我有一个包含truefalse语句的Xml配置文件。我需要一个返回bool值的function

代码语言:javascript
复制
<Configs>
     <steerWithMouse>true</steerWithMouse>
     <debug>false</debug>
</Configs>

该函数需要一个string值才能让节点进行后续操作。所以就像这样:

bool steerWithMouse = ReadConfigs("SteerWithMouse");

bool debug = ReadConfigs("debug");

代码语言:javascript
复制
public bool ReadConfigs(string config) {

        try {
            //Where the code should go.

        }
        catch {
            //If the program fails to find or load the file correctly.

            ModConsole.Error("Falied to load configs file."); //The console.
            steerWithMouse = true;
            debug = false;
        }
    }

由于我在Xml文件方面的经验不足,所以在尝试这样做时,我只会遇到挫折。它要么什么都没说,要么没有编译,要么返回了整个文档。我很乐意在这方面接受一些帮助。

EN

回答 1

Stack Overflow用户

发布于 2017-11-19 00:21:45

代码语言:javascript
复制
public bool ReadConfigs(string config) {

        string statement = null;

        try {
            string xmlFile = File.ReadAllText(ModLoader.GetModConfigFolder(this) + "\\configs.xml");
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xmlFile);
            XmlNodeList nodeList = xmldoc.GetElementsByTagName(config);
            string Short_Fall = string.Empty;
            foreach (XmlNode node in nodeList) {
                statement = node.InnerText;

            }
        }
        catch {
            ModConsole.Error("Falied to load configs file!");
            statement = null;
        }

        try {

            if (statement != null) {
                return bool.Parse(statement);
            }
            else
                return false;
        }
        catch {
            ModConsole.Error("Invalid syntax in configs!");
            return false;
        }
    }

This复制粘贴似乎已经解决了这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47366888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档