0

I have a Xml config file with true and false statements in it. I need a function which will return the bool value.

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

The function would need a string value for the node to go after. So something like this:

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;
        }
    }

Due to my inexperience in Xml files I have only meet frustration when trying to do this. It either didn't say anything, didn't compile or returned the whole document. I would gladly accept some help with this.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Simon
  • 95
  • 1
  • 11
  • What research did you do about how to read xml file and parse its data? Did you find anything useful? Did you apply that and try to solve this issue? – Chetan Nov 18 '17 at 14:02
  • Yes, I have been searching all day (yesterday) for a bunch of solutions. But none of those helped me. I erased it because of how messy my code where with commented out snippets and half made lines. – Simon Nov 18 '17 at 14:08
  • Have you considered letting [AppSettings](https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx) handle the reading part and just [TryParse](https://msdn.microsoft.com/en-us/library/system.boolean.tryparse(v=vs.110).aspx) the string value? – Filburt Nov 18 '17 at 14:09
  • Can you share whatever solution you tried and tell us what issue you are facing with that ? – Chetan Nov 18 '17 at 14:22
  • https://www.youtube.com/watch?v=M4aXKPN0nK0 https://www.youtube.com/watch?v=-DwANN5_BoE https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c http://csharp.net-informations.com/xml/how-to-read-xml.htm Just a few I read. – Simon Nov 18 '17 at 15:02
  • What I want is a function that reads a Xml file and returns the node that matches the search string as a bool. And if it fails to either find the file in question or doesn't find the matching node, it will print a error message and set the variables to the default settings. – Simon Nov 18 '17 at 15:30

1 Answers1

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 copy paste seems to have solved it.

Simon
  • 95
  • 1
  • 11