2017-11-18 4 views
0

Xml設定ファイルにtruefalseというステートメントが入っています。私はboolの値を返すfunctionが必要です。C#Read XML設定ファイル

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

この機能には、ノードが進むための値がstringである必要があります。したがって、このような何か:原因XMLファイルに私の経験不足に

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

これを実行しようとしたとき、私は唯一の不満を満たしています。それは何も言わなかったか、文書全体をコンパイルしたり返さなかったかのいずれかです。私は喜んでこれにいくつかの助けを受け入れるだろう。

+0

XMLファイルを読み込んでデータを解析する方法について、どのような研究を行っていますか?役に立つものは見つかりましたか?それを適用してこの問題を解決しようとしましたか? –

+0

はい、私は一晩中(昨日)複数のソリューションを探しています。しかし、それらのどれも私を助けませんでした。私はコードをどのように乱雑にしているのかを解読しました。 – Simon

+0

あなたは[AppSettings](https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v = vs.110).aspx)に読み込み部分を処理させ、[TryParse ](https://msdn.microsoft.com/en-us/library/system.boolean.typparse(v = vs.110).aspx)文字列の値は? – Filburt

答えて

0
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コピー貼り付けがそれを解決したようです。