0

我有这个 XML 文档,我正在尝试读取并插入到数据库中。只有一个 GamePlay 节点,但 GamePlayStep 可能会重复,所以我为此创建了一个节点列表。由于某种原因 GamePlayStep 没有接收数据。这是 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();


                }

            }
        }

GamePlay 正确填充变量并插入数据库,但 NodeList 中的 GamePlaySteps 不是。有没有人看到问题或我该如何改进?

谢谢。

4

2 回答 2

2

您访问 GamePlayStep 的 XPath 不正确;

它应该是:

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

因为 GamePlayStep 在您的文档中是 GameData 的子级,而不是 GamePlay。

于 2012-11-07T12:01:23.140 回答
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));
.....
}
于 2012-11-07T12:19:47.123 回答