Skip to main content

Microsoft Silverlight

Answered Question Clearing file content with XmlWriterRSS Feed

(0)

brettr
brettr

Member

Member

119 points

123 Posts

Clearing file content with XmlWriter

Is there a way to clear out a file using XmlWriter before writing the final content and without having to first delete the file?

pbrooks
pbrooks

Contributor

Contributor

2671 points

355 Posts

Silverlight MVP
Answered Question

Re: Clearing file content with XmlWriter

 brettr,

You could open the file in Truncate mode to clear the file while opening.  However, I believe this will only work if the file already exists.  See the code below:

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("test.xml", FileMode.Truncate, isoStore))
{
    XmlWriter w = XmlWriter.Create(isoStream);
    w.WriteStartElement("Test");
    w.WriteRaw("Stuff");
    w.WriteEndElement();
    w.Flush();
    isoStream.Flush();
    isoStream.Close();
}

If this has answered your question, please click on "Mark as Answer" on this post.

Thanks,
Page Brooks
Silverlight MVP, MCSD
PageBrooks.com | @pbrooks
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities