2017-11-18 1 views
0

truefalse 문을 포함하는 XML 구성 파일이 있습니다. bool 값을 반환하는 function이 필요합니다.C# XML 구성 파일 읽기

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

이 함수는 노드가 이동하기 위해 string 값이 필요합니다. 이 같은 뭔가 :

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

bool steerWithMouse = ReadConfigs("SteerWithMouse");

bool debug = ReadConfigs("debug"); 때문에 내 경험 부족에 XML 파일에이 작업을 수행 할 때 난 단지 좌절을 충족했다. 그것은 아무 것도 말하지 않았고, 전체 문서를 컴파일하거나 반환하지 않았습니다. 기꺼이이 문제에 대해 도움을 청합니다.

+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.tryparse (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 복사 붙여 넣기가 해결 된 것 같습니다.