|
|
Imports System.Xml |
|
Public Class DataAccess |
|
Private CurrentDirectory As String = |
System.IO.Directory.GetCurrentDirectory() & "\employee.xml" |
Private XMLDataFile As New XmlDocument |
|
|
|
Public Sub DataCreate(ByVal _employee As Employee) |
Dim oRoot As XmlNode = |
XMLDataFile.DocumentElement |
Dim oDocument As XmlElement = |
XMLDataFile.CreateElement("employee") |
Dim oID As XmlElement = |
XMLDataFile.CreateElement("id") |
Dim oName As XmlElement = |
XMLDataFile.CreateElement("name") |
Dim oBirthday As XmlElement = |
XMLDataFile.CreateElement("birthday") |
Dim oPostID As XmlElement = |
XMLDataFile.CreateElement("postid") |
|
oID.InnerText = CType(_employee.ID, String) |
oName.InnerText = _employee.Name |
oBirthday.InnerText = CType(_employee.Birthday, String) |
oPostID.InnerText = CType(_employee.PostID, String) |
|
With oDocument |
.AppendChild(oID) |
.AppendChild(oName) |
.AppendChild(oBirthday) |
.AppendChild(oPostID) |
End With |
|
oRoot.AppendChild(oDocument) |
XMLDataFile.Save(CurrentDirectory) |
End Sub |
|
|
|
Public Sub DataUpdate(ByVal Index As Integer, |
ByVal TaegetEmployee As Employee) |
With XMLDataFile |
.SelectNodes("//name").Item(Index).InnerText = |
TaegetEmployee.Name |
.SelectNodes("//birthday").Item(Index).InnerText = |
CType(TaegetEmployee.Birthday, String) |
.SelectNodes("//postid").Item(Index).InnerText = |
CType(TaegetEmployee.PostID, String) |
.Save(CurrentDirectory) |
End With |
End Sub |
|
|
|
Public Sub DataDelete(ByVal index As Integer) |
Dim TargetNode As XmlNode = |
XMLDataFile.SelectNodes("//root").Item(0) |
TargetNode.RemoveChild(TargetNode.ChildNodes(index)) |
XMLDataFile.Save(CurrentDirectory) |
End Sub |
|
|
|
Public Sub FileLoad(ByVal list As System.Collections.IList) |
XMLDataFile.Load(CurrentDirectory) |
Dim nID As XmlNodeList = |
XMLDataFile.SelectNodes("//id") |
Dim nName As XmlNodeList = |
XMLDataFile.SelectNodes("//name") |
Dim nBirthday As XmlNodeList = |
XMLDataFile.SelectNodes("//birthday") |
Dim nPostID As XmlNodeList = |
XMLDataFile.SelectNodes("//postid") |
|
Dim NodeIndex As Integer = 0 |
For Each EmployeeNode In XMLDataFile.SelectNodes("//employee") |
Dim oEmployee As New Employee |
With oEmployee |
.ID = CType(nID(NodeIndex).InnerText, Integer) |
.Name = nName(NodeIndex).InnerText |
.Birthday = CType(nBirthday(NodeIndex).InnerText, Date) |
.PostID = CType(nPostID(NodeIndex).InnerText, Integer) |
End With |
list.Add(oEmployee) |
NodeIndex += 1 |
Next |
End Sub |
|
|
|
Public Sub New() |
If System.IO.File.Exists(CurrentDirectory) = False Then |
Dim XMLFileFormat As String = |
"<?xml version=""1.0"" encoding=""utf-8""?><root></root>" |
System.IO.File.WriteAllText(CurrentDirectory, XMLFileFormat) |
End If |
End Sub |
End Class |
0 件のコメント:
コメントを投稿