我有一个包含true和false语句的Xml配置文件。我需要一个返回bool值的function。
<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文件方面的经验不足,所以在尝试这样做时,我只会遇到挫折。它要么什么都没说,要么没有编译,要么返回了整个文档。我很乐意在这方面接受一些帮助。
发布于 2017-11-19 00:21:45
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复制粘贴似乎已经解决了这个问题。
https://stackoverflow.com/questions/47366888
复制相似问题