【案内】小説『エクストリームセンス』について

 小説『エクストリームセンス』は、本ブログを含めていくつか掲載していますが、PC、スマフォ、携帯のいずれでも読みやすいのは、「小説家になろう」サイトだと思います。縦書きのPDFをダウンロードすることもできます。

 小説『エクストリームセンス』のURLは、 http://ncode.syosetu.com/n7174bj/

2011年10月23日日曜日

簡単なソフトウェアを作ってみよう(7)


簡単なソフトウェアを作ってみよう(4)で公開した、XMLファイルにデータをCRUDするロジックを書き直しました。
LINQ to XMLを使ってスッキリしました。
*ページ幅にコードを合わせるための改行により、可読性が落ちていますがご容赦ください。

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
'===========================================
'   データアクセス コンポーネント
'   最終更新日:2011年10月23日
'===========================================
‌ 
Imports System.Xml
‌ 
Public Class DataAccess
‌ 
    Private Structure ItemName
        Friend Const sEmployee As String = "employee"
        Friend Const sID As String = "id"
        Friend Const sName As String = "name"
        Friend Const sBirthday As String = "birthday"
        Friend Const sPostId As String = "postid"
    End Structure
‌ 
    Private DataFile As String =
        System.IO.Directory.GetCurrentDirectory() & "\employee.xml"
‌ 
    Private oXmlDoc As XDocument
‌ 
    '==============
    'DataCreateメソッド
    Public Sub DataCreate(ByVal TargetEmployee As Employee)
        Dim NewRecord As XElement =
            New XElement(New XElement(ItemName.sEmployee,
                             New XElement(ItemName.sID,
                                          TargetEmployee.ID),
                             New XElement(ItemName.sName,
                                          TargetEmployee.Name),
                             New XElement(ItemName.sBirthday,
                                          TargetEmployee.Birthday),
                             New XElement(ItemName.sPostId,
                                          TargetEmployee.PostID)))
        oXmlDoc.Root.Add(NewRecord)
        oXmlDoc.Save(DataFile)
    End Sub
‌ 
    '==============
    'DataUpdateメソッド
    Public Sub DataUpdate(ByVal TargetEmployee As Employee)
        Dim rs =
            From o In oXmlDoc.Descendants(ItemName.sEmployee)
                 Where o.Element(ItemName.sID).Value =
                 TargetEmployee.ID
        With rs.First
            .Element(ItemName.sID).Value =
                TargetEmployee.ID
            .Element(ItemName.sName).Value =
                TargetEmployee.Name
            .Element(ItemName.sBirthday).Value =
                TargetEmployee.Birthday
            .Element(ItemName.sPostId).Value =
                TargetEmployee.PostID
        End With
        oXmlDoc.Save(DataFile)
    End Sub
‌ 
    '==============
    'DataDeleteメソッド
    Public Sub DataDelete(ByVal TargetID As Integer)
        Dim rs =
            From o In oXmlDoc.Descendants(ItemName.sEmployee)
                 Where o.Element(ItemName.sID).Value =
                 CType(TargetID, String)
        rs.First.Remove()
        oXmlDoc.Save(DataFile)
    End Sub
‌ 
    '==============
    'FileLoadメソッド
    Public Sub FileLoad(ByVal list As System.Collections.IList)
        oXmlDoc = XDocument.Load(DataFile)
        Dim rs =
            From o In oXmlDoc.Descendants(ItemName.sEmployee)
        For Each v In rs
            Dim oEmployee As New Employee With {
            .ID = v.Element(ItemName.sID).Value,
            .Name = v.Element(ItemName.sName).Value,
            .Birthday = v.Element(ItemName.sBirthday).Value,
            .PostID = v.Element(ItemName.sPostId).Value
        }
            list.Add(oEmployee)
        Next
    End Sub
‌ 
    '==============
    'コンストラクタ
    Public Sub New()
        If System.IO.File.Exists(DataFile) = False Then
            Dim XMLFileFormat As String =
            "<?xml version=""1.0"" encoding=""utf-8""?><root></root>"
            System.IO.File.WriteAllText(DataFile, XMLFileFormat)
        End If
    End Sub
End Class

0 件のコメント:

コメントを投稿