LINQ to XML | Isolated Storage | Visual Basic 2010 | VB | Windows Phone 7 | WP7I am developing a
wp7 app which basically have to write, read to
xml file in
Isolated Storage of
windows phone 7. First and more important this post is for
Visual Basic 2010 NOT for C#. Please, do not paste a C# code and say just translate to
visual basic 2010, this does not work. Please, help and post code only if it is Visual Basic 2010 for
Windows Phone 7 |
WP7.
My questions are:
1. How to
create file?
2. How to
save it to
Isolated Storage?
3. How to
Open file?
4. How to
write to file and
add new rows?
5. How to
save(update) the file?
6. How to
read the file and make
queries from it?
Please, provide your solution. Bellow I am posting the code I have been done and does not work very well

1. Imports required libraries:
Imports System
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Linq
Imports System.Xml.Linq
Partial Public Class MainPage
Inherits PhoneApplicationPage
Dim xmlDoc As XDocument
Dim xmlFile As IsolatedStorageFileStream
Dim myStartShift As New DeliveryObj.StartShift
Dim myEndShift As New DeliveryObj.EndShift
' Constructor
Public Sub New()
InitializeComponent()
End Sub
2. Creating 2 classes StartShift and EndShift:
Class StartShift
Private dtStartShiftDateTime As DateTime
Public Property StartShiftDateTime() As DateTime
Get
Return dtStartShiftDateTime
End Get
Set(value As DateTime)
dtStartShiftDateTime = value
End Set
End Property
Public Sub StartShift(ByVal StartShiftDateTime As DateTime)
dtStartShiftDateTime = StartShiftDateTime
End Sub
End Class
Class EndShift
Private dtEndShiftDateTime As DateTime
Public Property EndShiftDateTime() As DateTime
Get
Return dtEndShiftDateTime
End Get
Set(value As DateTime)
dtEndShiftDateTime = value
End Set
End Property
Public Sub EndShift(ByVal EndShiftDateTime As DateTime)
dtEndShiftDateTime = EndShiftDateTime
End Sub
End Class
3. Connecting to the App Isolated Storage creating xml file and save it:
Public Sub xmlStorage()
Dim x As XDocument = _
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<user>
<startShift></startShift>
<endShift></endShift>
</user>
Using seStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
If seStorage.FileExists("1d.xml") Then
Using xmlFile As IsolatedStorageFileStream = seStorage.OpenFile("1d.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
xmlDoc = XDocument.Load(xmlFile)
End Using
Else
Using xmlFile As IsolatedStorageFileStream = seStorage.CreateFile("1d.xml")
x.Save(xmlFile) ' Here something breaking when saving file, but after restart debugger when file already exists everything works fine.
End Using
End If
End Using
4. Creating Button "Start Shift" = btnStartShift
Private Sub btnStartShift_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnStartShift.Click
btnStartShift.IsEnabled = False
btnEndShift.IsEnabled = True
myStartShift.StartShift(DateTime.Now)
Call xmlStorage()
Dim x = New XElement(<startShift><%= myStartShift.StartShiftDateTime %></startShift>)
xmlDoc.Root.LastNode.AddAfterSelf(x)
xmlDoc.Save(xmlFile) ' ArgumentNullException was unhandled. ==> Here is breaking
xmlDoc = XDocument.Load(xmlFile)
MessageBox.Show(xmlFile.ToString)
MessageBox.Show(xmlDoc.ToString)
End Sub
5. Creating Button "End Shift" = btnEndShift
Private Sub btnEndShift_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnEndShift.Click
btnStartShift.IsEnabled = True
btnEndShift.IsEnabled = False
myEndShift.EndShift(DateTime.Now)
Call xmlStorage()
Dim x = New XElement(<endShift><%= myEndShift.EndShiftDateTime %></endShift>)
xmlDoc.Root.LastNode.AddAfterSelf(x)
xmlDoc.Save(xmlFile) ' ArgumentNullException was unhandled. ==> Here is breaking
xmlDoc = XDocument.Load(xmlFile)
MessageBox.Show(xmlFile.ToString)
MessageBox.Show(xmlDoc.ToString)
End Sub
I am attaching the source code in case anyone want to debug it.
Thanks in advance for your cooperation and I will wait for your solution reply's.