XML, LINQ and XDocument.Save problems

selocan222

New Member
I'm having trouble saving xml files after making changes to them. I've spent all day today trying to figure this out and I've gotten nowhere.I have this xml doc:\[code\]<?xml version=1.0" encoding="utf-8"?><content> <weapon id="1234" name="blahblah"> <note info="blah blah" /> </weapon> <weapon id="5678" name="blahblah2"> <note info="blah blah" /> </weapon></content>\[/code\]This is what I've come up with so far that doesn't exactly work (Edited to show how I read file):\[code\]FileOpenPicker openPicker = new FileOpenPicker();openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;openPicker.FileTypeFilter.Add(".xml");StorageFile gfile = await openPicker.PickSingleFileAsync()fileContent = await FileIO.ReadTextAsync(gfile, Windows.Storage.Streams.UnicodeEncoding.Utf8);Xdocument xml = XDocument.Parse(fileContent);xml.Descendants("weapon").Where(c => c.Attribute("id").Value.Equals("5678")).FirstorDefault().Remove();IRandomAccessStream writeStream = await gfile.OpenAsync(FileAccessMode.ReadWrite);Stream stream = writeStream.AsStreamForWrite();xml.Save(stream);\[/code\]The resulting xml doc would be something like this:\[code\]<?xml version=1.0" encoding="utf-8"?><content> <weapon id="1234" name="blahblah"> <note info="blah blah" /></content>apon> <weapon id="5678" name="blahblah2"> <note info="blah blah" /> </weapon></content>\[/code\]If I try to use FileAccessMode.ReadWriteNoCopyOnWrite for OpenAsync the file ends up being 0 bytes.Anybody know how I can write this file correctly while still using XDocument.Save?
 
Back
Top