Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using XmlSerializer would be more efficient in this case instead of manually pushing elements inside the xml. Model your scorecard object and use basic Serializer and Deserializer. Following example should work (assuming your operation method is called SaveXmlValue()).</p> <pre><code>public class Match { public string Name { get; set; } public string Score { get; set; } } public class Scorecard { public List&lt;Match&gt; Match { get; set; } } public static Scorecard DeSerialize(Stream strm) { var ser = new XmlSerializer(typeof(Scorecard)); strm.Seek(0, SeekOrigin.Begin); return (Scorecard)ser.Deserialize(strm); } public static string Serialize(Scorecard scores) { var ser = new XmlSerializer(typeof(Scorecard)); using (var strm = new MemoryStream()) { var writer = XmlWriter.Create(strm, new XmlWriterSettings() { Indent = true, Encoding = System.Text.Encoding.UTF8 }); ser.Serialize(writer, scores); strm.Seek(0, SeekOrigin.Begin); var bytes = new byte[strm.Length]; strm.Read(bytes, 0, (int)strm.Length); return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length); } } public void SaveXmlValue() { Scorecard scores; if (isoStore.FileExists("scoreboard.xml")) { using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("scoreboard.xml", System.IO.FileMode.Open, isoStore)) { scores = DeSerialize(isoStream); } } else { // Xml file doesn't exist, create new score card object scores = new Scorecard(); scores.Match = new List&lt;Match&gt;(); } scores.Match.Add(new Match() { Name = VarGlobal.Name, Score = VarGlobal.Score }); var xml = Serialize(scores); using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("scoreboard.xml", System.IO.FileMode.Create, isoStore)) { var buffer = System.Text.Encoding.UTF8.GetBytes(xml); isoStream.Write(buffer, 0, buffer.Length); } } </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