forum

Home / DeveloperSection / Forums / C# XDocument Save copy file and appends edited to end

C# XDocument Save copy file and appends edited to end

Samuel Fernandes393218-Dec-2013

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>


Updated on 18-Dec-2013

Can you answer this question?


Answer

1 Answers

Liked By