2012-11-07 218 views
0

我有這個XML文檔,我試圖讀入並插入到數據庫中。只有一個GamePlay節點,但是GamePlayStep可能會重複,所以我爲此創建了一個節點列表。由於某些原因GamePlayStep沒有收到數據。下面是XML文件:通過XML節點讀取XML文件

<?xml version="1.0" encoding="utf-8" ?> 
<GameData> 
    <GamePlay> 
    <type>add</type> 
    <GamePlayID>1</GamePlayID> 
    <ParticipantID>1</ParticipantID> 
    <GameID>1</GameID> 
    <GameDifficultyID>1</GameDifficultyID> 
    <Start>2012-08-06T12:19:33.154Z</Start> 
    <End>2012-08-06T12:30:33.154Z</End> 
    <Success>False</Success> 
    </GamePlay> 
    <GamePlayStep> 
    <GamePlayStepID>1</GamePlayStepID> 
    <Start>2012-08-06T12:19:33.154Z</Start> 
    <End>2012-08-06T12:30:33.154Z</End> 
    <SortOrder>1</SortOrder> 
    <Score>1</Score> 
    <hintUsed>True</hintUsed> 
    <GamePause> 
     <GamePauseID>1</GamePauseID> 
     <Start>2012-08-06T12:19:33.154Z</Start> 
     <End>2012-08-06T12:30:33.154Z</End> 
     <Order>1</Order> 
     <Duration>05:01</Duration> 
    </GamePause> 
    </GamePlayStep> 
</GameData> 

這裏是我的代碼:

public static void start() 
     { 
      string[] filePaths = Directory.GetFiles(System.Configuration.ConfigurationManager.ConnectionStrings["filePath"].ConnectionString); 
      List<GamePlay> gameObj = new List<GamePlay>(); 
      List<GamePlayStep> gameStepObj = new List<GamePlayStep>(); 
      foreach (string value in filePaths) 
      { 
       XmlDocument xd = new XmlDocument(); 
       XmlNodeList GameSteps; 
       xd.Load(value); 
       XmlNode documentNode = xd.SelectSingleNode("/GameData/GamePlay"); 
       GameSteps = xd.SelectNodes("/GameData/GamePlay/GamePlayStep"); 

       GamePlay newGamePlay = new GamePlay(); 
       newGamePlay.setType(Convert.ToString(documentNode.SelectSingleNode("type").InnerText)); 
       newGamePlay.setGamePlayID(Convert.ToInt32(documentNode.SelectSingleNode("GamePlayID").InnerText)); 
       newGamePlay.setParticipantID(Convert.ToInt32(documentNode.SelectSingleNode("ParticipantID").InnerText)); 
       newGamePlay.setGameDifficultyID(Convert.ToInt32(documentNode.SelectSingleNode("GameDifficultyID").InnerText)); 
       newGamePlay.setGameID(Convert.ToInt32(documentNode.SelectSingleNode("GameID").InnerText)); 
       newGamePlay.setStartDateTime(Convert.ToDateTime(documentNode.SelectSingleNode("Start").InnerText)); 
       newGamePlay.setEndDateTime(Convert.ToDateTime(documentNode.SelectSingleNode("End").InnerText)); 
       newGamePlay.setSuccess(Convert.ToBoolean(documentNode.SelectSingleNode("Success").InnerText)); 
       newGamePlay.setFile(value); 
       newGamePlay.addNewGamePlay(); 

       foreach (XmlNode documentNode2 in GameSteps) 
       { 

        GamePlayStep newGamePlayStep = new GamePlayStep(); 
        newGamePlayStep.setGamePlayStepID(Convert.ToInt32(documentNode2.SelectSingleNode("GamePlayStepID").InnerText)); 
        newGamePlayStep.setGamePlayID(newGamePlay.getGamePlayID()); 
        newGamePlayStep.setStartDateTime(Convert.ToDateTime(documentNode2.SelectSingleNode("Start").InnerText)); 
        newGamePlayStep.setEndDateTime(Convert.ToDateTime(documentNode2.SelectSingleNode("End").InnerText)); 
        newGamePlayStep.setOrderPlayed(Convert.ToInt32(documentNode2.SelectSingleNode("SortOrder").InnerText)); 
        newGamePlayStep.setScore(Convert.ToInt32(documentNode2.SelectSingleNode("Score").InnerText)); 
        newGamePlayStep.setHintUsed(Convert.ToBoolean(documentNode2.SelectSingleNode("hintUsed").InnerText)); 
        newGamePlayStep.addNewGamePlayStep(); 


       } 

      } 
     } 

遊戲是正確填充變量並插入到數據庫但NodeList中GamePlaySteps都沒有。有沒有人看到這個問題,或者我可以如何改進?

謝謝。

回答

2

您訪問GamePlayStep的XPath不正確;

它應該是:

GameSteps = xd.SelectNodes("/GameData/GamePlayStep"); 

由於GamePlayStep是GAMEDATA,不是遊戲的孩子,在文檔中。

+0

'// GamePlayStep'將是簡單 – Anirudha

+0

@ Fake.It.Til.U.Make.It在這種情況下,無論是等價的,但在具有多個不同級別的GamePlayStep實例的文檔中,'//'會將它們全部返回。我經常發現最好使用最特定的XPath來避免副作用:-) – dash

1

使用LINQ2XML ....它的簡單和cool

XElement doc=XElement.Load("yourXml"); 
newGamePlay.setType(doc.Descendants("GameData").Element("GamePlay").Element("type").Value); 
.... 
foreach (Element eml in doc.Descendants("GameData").Elements("GamePlayStep")) 
{ 
GamePlayStep newGamePlayStep = new GamePlayStep(); 
newGamePlayStep.setGamePlayStepID(Convert.ToInt32(elm.Element("GamePlayStepID").Value)); 
newGamePlayStep.setStartDateTime(Convert.ToDateTime(elm.Element("Start").Value)); 
..... 
}