Note that there are some explanatory texts on larger screens.

plurals
  1. POdeserializing xml doc to a list of key value pairs
    text
    copied!<p>I am trying to deserialize a list of key value pairs that represent a list of high scores and initials. I keep getting an error when I try to deserialize the XML document: <code>There is an error in XML document (0, 0).</code></p> <p>I am deserializing my XML document as below:</p> <pre><code>GameHighScore highScoreHelper = new GameHighScore(); XmlSerializer deserializer = new XmlSerializer(typeof(GameHighScore)); highScoreHelper = (GameHighScore)deserializer.Deserialize(stream); for (int i = 0; i &lt; highScoreHelper.highScoreList.Count; i++) { highScore[i] = new KeyValuePair&lt;string, int&gt;(highScoreHelper.highScoreList[i].Initials, highScoreHelper.highScoreList[i].Score); } </code></pre> <p>Am I missing something in my helper class?</p> <pre><code> [XmlRootAttribute("GameScore")] public class GameHighScore { [XmlArray("Scores")] [XmlArrayItem("HighScore")] public List&lt;HighScoreStruct&lt;string, int&gt;&gt; highScoreList; private HighScoreStruct&lt;string, int&gt; scoreListHelper; [Obfuscation(Exclude = true)] public struct HighScoreStruct&lt;K, V&gt; { [XmlElement("Initials")] public K Initials { get; set; } [XmlElement("Score")] public V Score { get; set; } public HighScoreStruct(K initials, V score) : this() { Initials = initials; Score = score; } } /// &lt;summary&gt; /// The constructor instanstiates the highscore list and /// the high score struct. /// &lt;/summary&gt; public GameHighScore() { highScoreList = new List&lt;HighScoreStruct&lt;string, int&gt;&gt;(); scoreListHelper = new HighScoreStruct&lt;string, int&gt;(); } /// &lt;summary&gt; /// This method creates the list of scores to be stored to the disk. /// &lt;/summary&gt; /// &lt;param name="scoreList"&gt;&lt;/param&gt; public void CreateGameHighScore(List&lt;KeyValuePair&lt;string, int&gt;&gt; scoreList) { for (int i = 0; i &lt; scoreList.Count; i++) { scoreListHelper = new HighScoreStruct&lt;string, int&gt;(scoreList[i].Key, scoreList[i].Value); highScoreList.Add(scoreListHelper); } } } </code></pre> <p>Generated XML document </p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;GameScore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Scores&gt; &lt;HighScore&gt; &lt;Initials&gt;ZZZ&lt;/Initials&gt; &lt;Score&gt;53125&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;50000&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;45000&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;40000&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;35000&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;30000&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;25000&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;20000&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;15000&lt;/Score&gt; &lt;/HighScore&gt; &lt;HighScore&gt; &lt;Initials&gt;AAA&lt;/Initials&gt; &lt;Score&gt;10000&lt;/Score&gt; &lt;/HighScore&gt; &lt;/Scores&gt; &lt;/GameScore&gt; </code></pre> <p><strong>EDIT</strong></p> <p>The above XML document was generated by serializing my helper class.</p> <p>The <code>stream</code> is gotten from below from Nick Gravelyn's EasyStorage Library:</p> <pre><code>/// &lt;summary&gt; /// Loads a file. /// &lt;/summary&gt; /// &lt;param name="containerName"&gt;The name of the container from which to load the file.&lt;/param&gt; /// &lt;param name="fileName"&gt;The file to load.&lt;/param&gt; /// &lt;param name="loadAction"&gt;The load action to perform.&lt;/param&gt; public void Load(string containerName, string fileName, FileAction loadAction) { VerifyIsReady(); // lock on the storage device so that only one storage operation can occur at a time lock (storageDevice) { // open a container using (StorageContainer currentContainer = OpenContainer(containerName)) { // attempt the load using (var stream = currentContainer.OpenFile(fileName, FileMode.Open)) { loadAction(stream); } } } } </code></pre> <p>My full Load Method is below: Note: The save device is just an instance of my EasyStorage save device.</p> <pre><code>public static void Load() { if (Global.SaveDevice.IsReady) { if (Global.SaveDevice.FileExists(Global.containerName, Global.filename_highscores_xml)) { Global.SaveDevice.Load( Global.containerName, Global.filename_highscores_xml, stream =&gt; { try { //stream.Position = 0; GameHighScore highScoreHelper = new GameHighScore(); XmlSerializer deserializer = new XmlSerializer(typeof(GameHighScore)); highScoreHelper = (GameHighScore)deserializer.Deserialize(stream); for (int i = 0; i &lt; highScoreHelper.highScoreList.Count; i++) { highScore[i] = new KeyValuePair&lt;string, int&gt;(highScoreHelper.highScoreList[i].Initials, highScoreHelper.highScoreList[i].Score); } } catch (Exception e) { //if (!Global.SaveDevice.IsBusy &amp;&amp; Global.SaveDevice.IsReady) // Global.SaveDevice.DeleteAsync(Global.containerName, Global.filename_highscores_xml); //Save(); Logger.LogError("Score.Load", e); } }); } else { Save(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload