Sunday, September 29, 2013

Appending new XElement adds an entire XML to existing xml in stream

Appending new XElement adds an entire XML to existing xml in stream

I have an existing XML stored in the InternalFielStorage as..
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Books>
<Author name="Sam" />
</Books>
</Root>
I am trying to append a book node under the node but the when saved, I am
seeing a completly new xml added to the existing xml as..
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Books>
<Author name="Sam" />
</Books>
</Root>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Books>
<Author name="Sam" />
<Title>Test</Title>
</Books>
</Root>
Code I am using for this..

using (IsolatedStorageFile myStore =
IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream myStream = new
IsolatedStorageFileStream(App.FileName, FileMode.Open,
FileAccess.ReadWrite, myStore))
{
XDocument _xDoc = XDocument.Load(myStream);
XElement srcTree = new XElement("Title", "test");
_xDoc.Element("Root").Element("Books").Add(new XElement(srcTree));
_xDoc.Save(myStream);
QUESTIONS:
1. How can I avoid the new XML from being appended to the existing one?
2. How can I make the tab to under the tag?

Thanks in advance.

No comments:

Post a Comment