C# XDOCUMENT SAVE COPY FILE AND APPENDS EDITED TO END

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
C# 
C#
 2544  View(s)
Ratings:
Rate this:

When I'm trying to edit XML Element and save it, it generates copy (with edited element) and appends it to end of file.

var localStore = IsolatedStorageFile.GetUserStoreForApplication();

IsolatedStorageFileStream stream = new IsolatedStorageFileStream("DataFolder\\PlayerData.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, localStore);

var doc = XDocument.Load(stream);

doc.Root.Element("characters").Element("character").SetElementValue("expierence", 10);

doc.Save(stream, SaveOptions.None);

stream.Close();

Example output file:

<?xml version="1.0" encoding="utf-8"?>

<root>

    <characters>

        <character>

            <expierence>0</expierence>

        </character>

    </characters>

</root><?xml version="1.0" encoding="utf-8"?>

<root>

    <characters>

        <character>

            <expierence>10</expierence>

        </character>

    </characters>

</root>

  1. ezra heywood

    Post:145

    Points:1019
    Re: C# XDocument Save copy file and appends edited to end

    Hi Samuel,

    That's exactly what you told it to do by passing FileMode.OpenOrCreate.

    If you want to truncate any existing file, pass Create.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!