c# - LINQ to XML file not saving correctly -


i have method removes sync times older 10 sec's old , adds new 1 when fired:

    public void addsynctime()     {         //delete archived sync times         xmldocobject.descendants("lastsync")            .where(e => convert.todatetime(e.element("time").value) < datetime.now.addseconds(-10))            .where(e => e.element("id").value != "1")            .remove();         string t1 = xmldocobject.tostring();         //get last primary key added         var lastprimarykey = (from in xmldocobject.element("dataloadtimes").elements("lastsync").elements("id")                               select i).lastordefault().value;         //increment once         int newprimarykey = convert.toint32(lastprimarykey) + 1;         //create element insert         var newelement = new xelement("lastsync",                new xelement("id", newprimarykey),                new xelement("time", datetime.now.tostring()));         //add xml doc object         xmldocobject.element("dataloadtimes").add(newelement);         string t2 = xmldocobject.tostring();     } 

the above strings breakpoint testing.

this above code creates xml this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <!--qtabs data storage--> <dataloadtimes>   <lastsync>     <id>1</id>     <time>4/1/2015 10:29:13 am</time>   </lastsync>   <lastsync>     <id>2</id>     <time>4/1/2015 10:29:14 am</time>   </lastsync>   <lastsync>     <id>3</id>     <time>4/1/2015 10:29:15 am</time>   </lastsync> </dataloadtimes> 

here save method save xml after above fired:

    public async void savetoxmlfile()     {         //storagefile = await storagefolder.getfileasync(settings.xmlfile);         using (stream filestream = await storagefile.openstreamforwriteasync())         {             xmldocobject.save(filestream);         }     } 

now, when using 1st method few times , saving getting corrupted xml file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <!--qtabs data storage--> <dataloadtimes>   <lastsync>     <id>1</id>     <time>4/1/2015 10:23:53 am</time>   </lastsync>   <lastsync>     <id>2</id>     <time>4/1/2015 10:27:04 am</time>   </lastsync> </dataloadtimes>  <id>3</id>     <time>4/1/2015 10:26:43 am</time>   </lastsync>   <lastsync>     <id>4</id>     <time>4/1/2015 10:26:44 am</time>   </lastsync> </dataloadtimes> 

note line: </dataloadtimes> <id>3</id>

so what's going on here? debugging shows string attempting saved stored in xdocucument object right:

"<!--qtabs data storage-->\r\n<dataloadtimes>\r\n  <lastsync>\r\n    <id>1</id>\r\n    <time>4/1/2015 10:23:53 am</time>\r\n  </lastsync>\r\n  <lastsync>\r\n    <id>2</id>\r\n    <time>4/1/2015 10:27:04 am</time>\r\n  </lastsync>\r\n</dataloadtimes>" 

why file getting messed up?

i'm not familiar winrt, docs think need use:

await storagefolder.createfileasync("myxmlfile.xml", creationcollisionoption.replaceexisting); 

getfileasync open existing file as-is - allow read it's contents. contents writing smaller existing contents, remaining existing contents not touched.

if wanted stick getfileasync setting filestream.length = 0 before writing should fix issue you're seeing.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -