2014-12-20 1 views
-5

아래 스 니펫은 (int)winTempplayerWIn 변수의 합계가 아닙니다. createXML()에 전화하기 전에 화면에 인쇄하여 두 변수 모두에 올바른 값이 할당되었는지 확인했습니다. 제 이론은 새로운 XELEMENT를 만들면서 방정식을 평가할 수 없다는 것입니다. 누구든지 이것을 확인할 수 있습니까? 내가 saveXML()에서 주석 처리 된 줄과 같은 XElement, 외부에서 할 경우 의도 한대로"새 EXlement"동안 방정식 계산

new XElement("playerWin", (int)winTemp + playerWin), 

, 그것은 작동합니다.

  • 파일이없는 경우 - 예외가있는 XML 출력은 Wins = 10, Loss = 1, Tie = 0이어야합니다.
  • 파일이 존재하는 경우 - 예외가있는 XML 출력은 Wins = 20, Loss = 2, Tie = 0이어야합니다.

번호 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Xml.Linq; 
using System.Text; 
using System.IO; 
using System.Threading.Tasks; 

namespace Testing_LINQ_to_XML 
{ 
    class Program 
    { 
     static void Main() 
     { 
      Player p = new Player(); 
      p.readXML(); 
      p.toScreen(); 
      p.saveXML(); 
      p.readXML(); 
      p.toScreen(); 
      p.Exit(); 
     } 
    } 

    public class Player 
    { 
     public string path; 
     public string playerName; 
     public int playerWin = 10; 
     public int playerLoss = 1; 
     public int playerTie = 0; 
     public int winTemp; 
     public int lossTemp; 
     public int tieTemp; 

     public Player() 
     { 
      Console.WriteLine("Enter player Name..."); 
      playerName = Console.ReadLine(); 
      Console.WriteLine("n: " + playerName); 
      getPath(); 
      Console.WriteLine("p: " + path); 
      Console.ReadLine(); 
     } 

     public string getPath() 
     { 
      path = (@"..\XML Saves\" + playerName + ".xml"); 
      return path; 
     } 

     public void toScreen() 
     { 
      Console.WriteLine("\nYour Record Is:\n"); 
      Console.WriteLine("Wins:  " + playerWin); 
      Console.WriteLine("Losses: " + playerLoss); 
      Console.WriteLine("Ties:  " + playerTie); 
     } 

     public void saveXML() 
     { 
      if (File.Exists(path)) 
      { 
       readXML(); 
       File.Delete(path); 
       //playerWin = (int)winTemp; 
       //playerLoss = (int)lossTemp; 
       //playerTie = (int)tieTemp; 
       createFile(); 
      } 
      else 
      { 
       createFile();     
      }   
     } 

     public void createFile() 
     { 
       XDeclaration _obj = new XDeclaration("1.0", "utf-8", ""); 
       XNamespace gameSaves = "gameSaves"; 
       XElement fileNew = new XElement("Root", 
            new XElement("Player", 
             new XElement("playerName", playerName), 
             new XElement("Stats", 
              new XElement("playerWin", (int)winTemp + playerWin), 
              new XElement("playerLoss", (int)lossTemp + playerLoss), 
              new XElement("playerTie", (int)tieTemp + playerTie)))); 


       fileNew.Save(path); 
       Console.WriteLine("Save created: " + path); 
     } 

     public void readXML() 
     { 
      if (File.Exists(path)) 
      { 
       var winTemp = new XElement("playerWin", playerWin); 
       var lossTemp = new XElement("playerLoss", playerLoss); 
       var tieTemp = new XElement("playerTie", playerTie); 
      } 
      else 
      { 
       Console.WriteLine("\nYou don't have any stats to show yet. Get playing!!!"); 
      } 

     } 

     public void Exit() 
     { 
      Console.WriteLine("Press any key to exit."); 
      Console.ReadLine(); 
     } 
    } 
} 

XML 출력 :

<Root> 
    <Player> 
     <playerName>Name</playerName> 
     <Stats> 
     <playerWin>10</playerWin> 
     <playerLoss>1</playerLoss> 
     <playerTie>0</playerTie> 
     </Stats> 
    </Player> 
</Root> 
+0

변수의 정의와 지금 생성되는 코드와 예상과 다른 코드를 추가하십시오. 지금 당장은 무엇이 잘못되었는지를 밝히기가 어렵습니다. – jessehouwing

+0

물론 XML도 마찬가지입니다. 기본적으로, 우리는 당신을 돕기 위해 문제를 재현 할 수 있어야합니다. –

+0

이해할 수없는 질문 -보다 집중된 샘플과 올바른 질문이 필요합니다. –

답변

0

로컬로 winTemp 변수 (readXML()있어서 내부) 인스턴스 변수 숨어 범위. 따라서 winTemp 인스턴스 변수는 전혀 설정되지 않으며 추가시 기본값 인 0을 유지합니다.