I have been fiddling with this problem for the past hour so I thought you guys may be able to help on a Friday afternoon!
Problem
I am trying to edit an XML file in localstorage but can't figure out how to edit the existing file and re-save the file. The edit I have made it to remove a certain node from the XML.
Code
Here is the method that does all the work.
This first code snippet was already in the code and basically creates the XML file and saves it to localstorage.:
protected byte[] CreateFileData(PortableBusinessObjects.Location location, string geoObjectFilename) { byte[] fileData = null; var xmlFile = System.IO.Path.GetFileNameWithoutExtension(geoObjectFilename) + ".xml"; var zipFile = System.IO.Path.GetFileNameWithoutExtension(geoObjectFilename) + ".zip"; using (IsolatedStorageFileStream fileStream = localStorage.CreateFile(xmlFile)) { XmlWriter writer = XmlWriter.Create(fileStream); if (location.GetType() == typeof(PortableBusinessObjects.Shape)) _xmlShapeSerializer.Serialize(writer, location); else if (location.GetType() == typeof(PortableBusinessObjects.Point)) _xmlPointSerializer.Serialize(writer, location); fileStream.Flush(); fileStream.Close(); } } This is my attempt at overwriting the saved file (Doesn't work):
using (IsolatedStorageFileStream doc = localStorage.OpenFile(xmlFile, FileMode.Open)) { System.Xml.Linq.XDocument test = System.Xml.Linq.XDocument.Load(doc); test.Descendants("Time").Remove(); XmlWriter writer = XmlWriter.Create(doc); doc.Flush(); doc.Close(); } Question
Where do I place my code that removes the "Time" nodes and saves the file?
No comments:
Post a Comment