[Windows Phone 7 Software Development] GFR_2009:
Hi there,

I'm having a big issue, when trying to remove an XElement from an XML file created in IsolatedStorage.

--------------------------------------------------------------------------------------------

Code to CREATE the XML file

Dim File_to_Create As String = "Tracks.xml"

Dim file As XDocument = <?xml version="1.0" encoding="UTF-8"?>

<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Cartridges.xsd" generated="2010-11-23T14:26:55">

<Carts>

<CART_NAME>First</CART_NAME>

<CART_COLOR>White</CART_COLOR>

</Carts>

<Carts>

<CART_NAME>Second</CART_NAME>

<CART_COLOR>Black</CART_COLOR>

</Carts>

</dataroot>

Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()

Try

If isoStore.FileExists(File_to_Create) Then

MessageBox.Show(File_to_Create + " TRUE")

Else

MessageBox.Show(File_to_Create + " FALSE")

Dim oStream As New IsolatedStorageFileStream(File_to_Create, FileMode.Create, isoStore)

Dim writer As New StreamWriter(oStream)

writer.WriteLine(file)

writer.Close()

MessageBox.Show("OK")

End If

Catch ex As Exception

MessageBox.Show(ex.Message)

Finally

'open selected file

Dim isoStream As IsolatedStorageFileStream

isoStream = New IsolatedStorageFileStream(File_to_Create, System.IO.FileMode.Open, System.IO.FileAccess.Read, isoStore)

Dim XML_File As XDocument = XDocument.Load(isoStream)

Dim Cart_Query As System.Collections.IEnumerable = From query In XML_File.Descendants("Carts") Order By _

CStr(query.Element("CART_NAME")) Descending, CStr(query.Element("CART_NAME"))

Select New Class_Cartridge_Data With {.Cart_Name = CStr(query.Element("CART_NAME")), _

.Cart_Color = CStr(query.Element("CART_COLOR"))}

Me.ListBox_Cartridges.ItemsSource = Cart_Query

isoStore.Dispose()

isoStream.Close()

End Try

--------------------------------------------------------------------------------------------

Code to ADD / EDIT XElement

Dim File_to_Create As String = "Tracks.xml"

Dim XML_IsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()

' Check that the file exists if not create it

If Not (XML_IsolatedStorage.FileExists(File_to_Create)) Then

Return

End If

Dim XML_StreamReader As New StreamReader(XML_IsolatedStorage.OpenFile(File_to_Create, FileMode.Open, FileAccess.Read))

Dim XML_Document As XDocument = XDocument.Parse(XML_StreamReader.ReadToEnd())

XML_StreamReader.Close()

' Update the element if it exist or create it if it doesn't

Dim XML_XElement As XElement = XML_Document.Descendants("Carts").Where(Function(c) c.Element("CART_NAME").Value.Equals("First")).FirstOrDefault()

If XML_XElement IsNot Nothing Then

XML_XElement.SetElementValue("CART_NAME", "Third")

Else

' Add new

Dim newProgress As New XElement("Cartridges", New XElement("CART_NAME", "Fourth"), New XElement("CART_COLOR", "Blue"))

Dim rootNode As XElement = XML_Document.Root

rootNode.Add(newProgress)

End If

Using XML_StreamWriter As New StreamWriter(XML_IsolatedStorage.OpenFile(File_to_Create, FileMode.Open, FileAccess.Write))

XML_StreamWriter.Write(XML_Document.ToString())

XML_StreamWriter.Close()

End Using

--------------------------------------------------------------------------------------------

Now my issue and request for some help!

If I use

XML_XElement.Remove

then the following exception is raised whenever I try to "refresh" the bounded ListBox

System.Xml.XmlException was unhandled

LineNumber=37

LinePosition=12

Message=Data at the root level is invalid. Line 37, position 12.

SourceUri=""

StackTrace:

at System.Xml.XmlTextReaderImpl.Throw(Exception e)

at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)

at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString)

at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()

at System.Xml.XmlTextReaderImpl.ParseDocumentContent()

at System.Xml.XmlTextReaderImpl.Read()

at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)

at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)

at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)

at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options)

at System.Xml.Linq.XDocument.Load(Stream stream)

at ListBox_Data_from_XML_LINQ.MainPage.Button_Create_XML_Click(Object sender, RoutedEventArgs e)

at System.Windows.Controls.Primitives.ButtonBase.OnClick()

at System.Windows.Controls.Button.OnClick()

at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)

at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)

at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

InnerException:

--------------------------------------------------------------------------------------------

In short, I can add or edit, but cannot DELETE an XElement...

Any ideas?

Thanks in advance!