Question

I have this XML document I am trying to read in and insert into a data base. There will only ever be one GamePlay node however GamePlayStep may repeat so I have created a node list for this. For some reason GamePlayStep isn't receiving data. Here is the XML file:

<?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>

Here is my code:

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();


                }

            }
        }

The GamePlay is filling the variables properly and inserting into the database however the GamePlaySteps in the NodeList are not. Does anyone see the problem or how I can improve this?

Thanks.

Was it helpful?

Solution

Your XPath for accessing the GamePlayStep is incorrect;

It should be:

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

As GamePlayStep is a child of GameData, not GamePlay, in your document.

OTHER TIPS

Use LINQ2XML....its simple and 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));
.....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top