Tu máte, ako som editáciu XML riešil ja, možno je to prasáreň, ale fungovalo mi to.
REM Načítanie
Private Sub LoadInternal(ByVal doc As XDocument)
Dim examinersUI As ListBox = My.Forms.MainForm.ExaminerView1.ExaminerList
Dim examinersNode As XElement = doc.<Evidence>(0).<Examiners>(0)
For Each examinerNode As XElement In examinersNode.<Examiner>
Dim id As Integer = Int32.Parse(examinerNode.<ID>(0).Value)
Dim fn As String = examinerNode.<FirstName>(0).Value
Dim sn As String = examinerNode.<SurName>(0).Value
Dim a As String = examinerNode.<Address>(0).Value
Dim c As String = examinerNode.<City>(0).Value
Dim z As String = examinerNode.<ZipCode>(0).Value
Dim p As String = examinerNode.<Phone>(0).Value
Dim examiner As New Examiner(fn, sn, a, c, z, p)
examiner.ID = id
examinersUI.Items.Add(examiner)
Next
examinersUI.Invalidate()
Dim gradesUI As TreeNodeCollection = My.Forms.MainForm.StudentControl1.StudentView1.Nodes
Dim gradesNode As XElement = doc.<Evidence>(0).<Grades>(0)
For Each GradeNode As XElement In gradesNode.<Grade>
Dim g As New GradeNode(GradeNode.<Name>(0).Value)
Dim studsNode As XElement = GradeNode.<Students>(0)
For Each StudNode As XElement In studsNode.<Student>
Dim s As New StudentNode()
s.FirstName = StudNode.<FirstName>(0).Value
s.SurName = StudNode.<SurName>(0).Value
s.MotherFirstName = StudNode.<MFirstName>(0).Value
s.FatherFirstName = StudNode.<FFirstName>(0).Value
s.Address = StudNode.<Address>(0).Value
s.City = StudNode.<City>(0).Value
s.ZipCode = StudNode.<ZipCode>(0).Value
s.StudentPhone = StudNode.<SPhone>(0).Value
s.MotherPhone = StudNode.<MPhone>(0).Value
s.FatherPhone = StudNode.<FPhone>(0).Value
Dim gen As Integer = Int32.Parse(StudNode.<Gender>(0).Value)
s.Gender = IIf(gen = 0, Gender.Muž, Gender.Žena)
s.Birth = DateTime.Parse(StudNode.<Birth>(0).Value)
s.Integrated = Boolean.Parse(StudNode.<Integrated>(0).Value)
Dim examsNode As XElement = StudNode.<Exams>(0)
For Each examNode As XElement In examsNode.<Exam>
Dim type As ExamType = [Enum].Parse(GetType(ExamType), examNode.<Type>(0))
Dim ed As Date = DateTime.Parse(examNode.<ExamDate>(0).Value)
Dim vd As Date = DateTime.Parse(examNode.<ValidDate>(0).Value)
Dim eid As Integer = Int32.Parse(examNode.<Examiner>(0).Value)
Dim e As Examiner = Nothing
For Each ex As Examiner In examinersUI.Items
If ex.ID = eid Then
e = ex
Exit For
End If
Next
If e Is Nothing Then
Throw New FormatException("Invalid input file. Could not load examiner of exam.")
End If
Dim examExaminer As New ExamExaminer(e, type, ed, vd, s)
s.Exams.Add(examExaminer)
Next
g.Nodes.Add(s)
Next
gradesUI.Add(g)
Next
End Sub
Public Sub Load(ByVal filename As String)
Dim dlg As New InputBox
dlg.Prompt = "Zadajte heslo"
dlg.TextLabel.PasswordChar = "*"
If dlg.ShowDialog() = DialogResult.OK Then
My.Forms.MainForm.StudentControl1.StudentView1.Nodes.Clear()
My.Forms.MainForm.ExaminerView1.ExaminerList.Items.Clear()
Using fs As New FileStream(filename, FileMode.Open)
Dim doc = Decrypt(fs, dlg.SelectedText)
If doc IsNot Nothing Then
LoadInternal(doc)
End If
End Using
End If
End Sub
Private Function Decrypt(ByVal source As Stream, ByVal password As String) As XDocument
Try
Dim passhash As Byte()
Using sha As New SHA512Managed()
passhash = sha.ComputeHash(Encoding.Default.GetBytes(password))
Using pdb As New Rfc2898DeriveBytes(password, passhash)
Dim key = pdb.GetBytes(32)
Dim iv = pdb.GetBytes(16)
Using Aes As New RijndaelManaged()
Using crypto = Aes.CreateDecryptor(key, iv)
Using Stream As New CryptoStream(source, crypto, CryptoStreamMode.Read)
Return XDocument.Load(Stream)
End Using
End Using
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show("Neviem načítať súbor. Skontrolujte správnosť zadaného hesla.", My.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
End Try
End Function
REM Ukladanie
Private Function SaveExaminer(ByVal ex As Examiner) As XElement
Return <Examiner>
<ID><%= ex.ID %></ID>
<FirstName><%= ex.Firstname %></FirstName>
<SurName><%= ex.SurName %></SurName>
<Address><%= ex.Address %></Address>
<City><%= ex.City %></City>
<ZipCode><%= ex.ZipCode %></ZipCode>
<Phone><%= ex.Phone %></Phone>
</Examiner>
End Function
Private Function SaveExaminers(ByVal ex As ListBox.ObjectCollection) As XElement
Dim node = <Examiners>
</Examiners>
For Each e As Examiner In ex
node.Add(SaveExaminer(e))
Next
Return node
End Function
Private Function SaveExam(ByVal ex As ExamExaminer) As XElement
Return <Exam>
<Type><%= ex.Type %></Type>
<ExamDate><%= ex.ExamDate.ToShortDateString() %></ExamDate>
<ValidDate><%= ex.ValidDate.ToShortDateString() %></ValidDate>
<Type><%= ex.Type %></Type>
<Examiner><%= ex.Examiner.ID %></Examiner>
</Exam>
End Function
Private Function SaveExams(ByVal ex As List(Of ExamExaminer)) As XElement
Dim node = <Exams>
</Exams>
For Each e As ExamExaminer In ex
node.Add(SaveExam(e))
Next
Return node
End Function
Private Function SaveStudent(ByVal ex As StudentNode) As XElement
Dim node = <Student>
<FirstName><%= ex.FirstName %></FirstName>
<MFirstName><%= ex.MotherFirstName %></MFirstName>
<FFirstName><%= ex.FatherFirstName %></FFirstName>
<SurName><%= ex.SurName %></SurName>
<Address><%= ex.Address %></Address>
<City><%= ex.City %></City>
<ZipCode><%= ex.ZipCode %></ZipCode>
<SPhone><%= ex.StudentPhone %></SPhone>
<MPhone><%= ex.MotherPhone %></MPhone>
<FPhone><%= ex.FatherPhone %></FPhone>
<Gender><%= CInt(ex.Gender) %></Gender>
<Birth><%= ex.Birth.ToShortDateString() %></Birth>
<Integrated><%= ex.Integrated %></Integrated>
</Student>
node.Add(SaveExams(ex.Exams))
Return node
End Function
Private Function SaveStudents(ByVal ex As TreeNodeCollection) As XElement
Dim node = <Students>
</Students>
For Each e As StudentNode In ex
node.Add(SaveStudent(e))
Next
Return node
End Function
Private Function SaveGrades(ByVal ex As TreeNodeCollection) As XElement
Dim node = <Grades>
</Grades>
For Each e As GradeNode In ex
Dim nodeInternal = <Grade>
<Name><%= e.GradeName %></Name>
</Grade>
nodeInternal.Add(SaveStudents(e.Nodes))
node.Add(nodeInternal)
Next
Return node
End Function
Private Function SaveInternal() As XDocument
Dim doc = <?xml version="1.0"?><Evidence></Evidence>
Dim evid = doc.Root
evid.Add(SaveGrades(My.Forms.MainForm.StudentControl1.StudentView1.Nodes))
evid.Add(SaveExaminers(My.Forms.MainForm.ExaminerView1.ExaminerList.Items))
Return doc
End Function
Je to na inom dátovom modeli, ale ako návod to snáď postačí.
|