Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I append information in an XML file using LINQ in C#?
    primarykey
    data
    text
    <p>I'm trying to write some information in an XML file, the structure should be:</p> <pre><code>&lt;Scoreboard&gt; &lt;Match&gt; &lt;name&gt;Dummy&lt;/name&gt; &lt;score&gt;1234&lt;/score&gt; &lt;/Match&gt; &lt;Match&gt; &lt;name&gt;Sample&lt;/name&gt; &lt;score&gt;4567&lt;/score&gt; &lt;/Match&gt; &lt;/Scoreboard&gt; </code></pre> <p>The problem is that I'm only able to write one entry, if I insert another "Match" the old one gets overwritten.</p> <p>For example, if I have:</p> <pre><code>&lt;Scoreboard&gt; &lt;Match&gt; &lt;name&gt;Dummy&lt;/name&gt; &lt;score&gt;1234&lt;/score&gt; &lt;/Match&gt; &lt;/Scoreboard&gt; </code></pre> <p>And then I want to add another entry the old one will be deleted and I'll only have the new one, for example:</p> <pre><code>&lt;Scoreboard&gt; &lt;Match&gt; &lt;name&gt;Sample&lt;/name&gt; &lt;score&gt;4567&lt;/score&gt; &lt;/Match&gt; &lt;/Scoreboard&gt; </code></pre> <p>I would know how can I write new entries without overwriting the old ones.</p> <p>Here's my code:</p> <pre><code>using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { //If file doesn't exist, then create it. if (!isoStore.FileExists("scoreboard.xml")) { XDocument doc = new XDocument(new XElement("Scoreboard")); using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("scoreboard.xml", System.IO.FileMode.Create, isoStore)) { doc.Save(isoStream); } } else { //Else open it and write a new element which is a child of Scoreboard using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("scoreboard.xml", System.IO.FileMode.Open, isoStore)) { XDocument doc1 = XDocument.Load(isoStream); var newElement = new XElement("Match", new XElement("name", VarGlobal.Name), new XElement("score", VarGlobal.Score)); doc1.Element("Scoreboard").Add(newElement); Messaggio.Text = doc1.ToString(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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