【问题标题】:C# Read Xml config fileC# 读取 Xml 配置文件
【发布时间】:2023-04-01 11:50:01
【问题描述】:

我有一个 Xml 配置文件,其中包含 truefalse 语句。我需要一个function,它将返回bool 值。

<Configs>
     <steerWithMouse>true</steerWithMouse>
     <debug>false</debug>
</Configs>

该函数需要一个string 值才能让节点执行。所以是这样的:

bool steerWithMouse = ReadConfigs("SteerWithMouse");

bool debug = ReadConfigs("debug");

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 文件方面缺乏经验,我在尝试执行此操作时只会遇到挫折。它要么什么也没说,要么没有编译,要么返回了整个文档。我很乐意接受一些帮助。

【问题讨论】:

标签: c# xml io config


【解决方案1】:
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复制粘贴似乎已经解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    相关资源
    最近更新 更多