INIファイルは簡単にプログラムの動作パラメータなどを保存しておくには便利な機能です。 この機能をXMLファイルで出来る様にクラスを作成してみました。
INIファイルへの書込み、読み込みの様な関数を持ったクラスになります。 以下にそのクラスのソースを示します。
XMLファイルをINIファイルの様に読み書きクラス
Imports System.Xml
Imports System.Text
Public Class ClsXmlIni
Private XmlDoc As XmlDocument
Private XmlFileName As String
'''
''' コンストラクタ
'''
''' <param name="strXmlFilePath">XMLファイル</param>
Public Sub New(ByVal strXmlFilePath As String)
Try
'初期XMLファイル読込
Me.XmlFileName = strXmlFilePath
Me.XmlDoc = New XmlDocument
If System.IO.File.Exists(strXmlFilePath) = False Then
'XML宣言を設定する
Dim xmlDecl As System.Xml.XmlDeclaration = XmlDoc.CreateXmlDeclaration("1.0", "Shift-JIS", Nothing)
'作成したXML宣言をDOMドキュメントに追加します
XmlDoc.AppendChild(xmlDecl)
Else
'ファイルが存在した場合
XmlDoc.Load(strXmlFilePath) 'XMLファイルをロード
End If
Catch ex As Exception
Throw ex
End Try
End Sub
'''
''' プロファイル文字列の取得
'''
''' <param name="strAppName">アプリケーション文字列</param>
''' <param name="strKeyName">キー文字列</param>
''' <param name="strDefault">デフォルト文字列</param>
''' <returns>プロファイル文字列</returns>
Public Function GetProfileString(ByVal strAppName As String, _
ByVal strKeyName As String, _
ByVal strDefault As String) As String
'戻り値初期化
GetProfileString = strDefault
Try
'引数チェック
If strAppName = "" Or strKeyName = "" Then Exit Function
'INIノードチェック
Dim xeleIni As XmlElement = Me.XmlDoc.SelectSingleNode("INI")
If Not xeleIni Is Nothing Then
'APPチェック
Dim xeleApp As XmlElement = xeleIni.SelectSingleNode(strAppName)
If Not xeleApp Is Nothing Then
'KEYチェック
Dim xeleKey As XmlElement = xeleApp.SelectSingleNode(strKeyName)
If Not xeleKey Is Nothing Then
GetProfileString = xeleKey.InnerText
End If
End If
End If
Catch ex As Exception
Throw ex
End Try
End Function
'''
''' プロファイル文字列設定
'''
''' <param name="strAppName">アプリケーション文字列</param>
''' <param name="strKeyName">キー文字列</param>
''' <param name="strSet">設定文字列</param>
''' <returns>True:正常, False:エラー</returns>
Public Function WriteProfileString(ByVal strAppName As String, _
ByVal strKeyName As String, _
ByVal strSet As String) As Boolean
'戻り値初期化
WriteProfileString = False
Try
'引数チェック
If strAppName = "" Or strKeyName = "" Then Exit Function
'INIノードチェック
Dim xeleIni As XmlElement = Me.XmlDoc.SelectSingleNode("INI")
If xeleIni Is Nothing Then
xeleIni = Me.XmlDoc.CreateElement("INI") 'INI要素を作成
Call Me.XmlDoc.AppendChild(xeleIni) 'ドキュメントに追加
End If
'APPチェック
Dim xeleApp As XmlElement = xeleIni.SelectSingleNode(strAppName)
If xeleApp Is Nothing Then
xeleApp = Me.XmlDoc.CreateElement(strAppName) 'APP要素を作成
Call xeleIni.AppendChild(xeleApp) 'INI要素に追加
End If
'KEYチェック
Dim xeleKey As XmlElement = xeleApp.SelectSingleNode(strKeyName)
If xeleKey Is Nothing Then
xeleKey = Me.XmlDoc.CreateElement(strKeyName) 'KEY要素を作成
Dim xValKey As XmlText = Me.XmlDoc.CreateTextNode(strSet) '設定値を作成
Call xeleKey.AppendChild(xValKey) 'KEY要素に値を追加
Call xeleApp.AppendChild(xeleKey) 'APP要素に追加
Else
'KEYが存在
xeleKey.InnerText = strSet
End If
'XMLファイルの上書き
Me.XmlDoc.Save(Me.XmlFileName)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
このクラスを利用する例を以下に記します。 フォーム(frmXmlIni)の上にボタンを貼り付け、このボタン処理内で、 最初にXMLファイルへの書込を行い、その後でそのXMLファイルからの読込を行っています。 その後さらに、書込と読込を行っています。
Public Class frmXmlIni
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
'XMLファイルのテスト
Dim strPath As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
Dim Ini As New ClsXmlIni(strPath & "\TEST.XML")
'書込テスト
Ini.WriteProfileString("SECTION1", "DATA1", "aaa")
Ini.WriteProfileString("SECTION1", "DATA2", "bbb")
Ini.WriteProfileString("SECTION2", "DATA1", "AAA")
'読込テスト
Dim strDsp As String = ""
strDsp &= "SECTION1:DATA1=" & Ini.GetProfileString("SECTION1", "DATA1", "***") & vbCrLf
strDsp &= "SECTION1:DATA1=" & Ini.GetProfileString("SECTION1", "DATA2", "") & vbCrLf
strDsp &= "SECTION1:DATA1=" & Ini.GetProfileString("SECTION2", "DATA1", "@@@") & vbCrLf
MsgBox(strDsp)
'書込テスト
Ini.WriteProfileString("SECTION1", "DATA1", "111")
Ini.WriteProfileString("SECTION1", "DATA2", "222")
Ini.WriteProfileString("SECTION2", "DATA1", "333")
'読込テスト
strDsp = ""
strDsp &= "SECTION1:DATA1=" & Ini.GetProfileString("SECTION1", "DATA1", "") & vbCrLf
strDsp &= "SECTION1:DATA1=" & Ini.GetProfileString("SECTION1", "DATA2", "") & vbCrLf
strDsp &= "SECTION1:DATA1=" & Ini.GetProfileString("SECTION2", "DATA1", "") & vbCrLf
MsgBox(strDsp)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
この処理の後のXMLファイルの内容は以下の様になります。
<?xml version="1.0" encoding="Shift-JIS"?>
<INI>
<SECTION1>
<DATA1>111</DATA1>
<DATA2>222</DATA2>
</SECTION1>
<SECTION2>
<DATA1>333</DATA1>
</SECTION2>
</INI>
PR
コメント
1. 参考になりました
ClsXmlIniクラスをnewをしているところですが、ファイルが無い場合は初期化してますが、存在している場合の処理が無かったです。
If System.IO.File.Exists(strXmlFilePath) = False Then
'XML宣言を設定する
Dim xmlDecl As System.Xml.XmlDeclaration = XmlDoc.CreateXmlDeclaration("1.0", xEncode.BodyName, Nothing)
'作成したXML宣言をDOMドキュメントに追加します
XmlDoc.AppendChild(xmlDecl)
Else
XmlDoc = New XmlDocument
XmlDoc.Load(strXmlFilePath) 'XMLファイルをロード
End If
こんな感じでうまく動きました。
2. 参考になりました
ClsXmlIniクラスをnewをしているところですが、ファイルが無い場合は初期化してますが、存在している場合の処理が無かったです。
If System.IO.File.Exists(strXmlFilePath) = False Then
'XML宣言を設定する
Dim xmlDecl As System.Xml.XmlDeclaration = XmlDoc.CreateXmlDeclaration("1.0", xEncode.BodyName, Nothing)
'作成したXML宣言をDOMドキュメントに追加します
XmlDoc.AppendChild(xmlDecl)
Else
XmlDoc = New XmlDocument
XmlDoc.Load(strXmlFilePath) 'XMLファイルをロード
End If
こんな感じでうまく動きました。
3. ご指摘有難うございます。
また何かありましたら、宜しくお願い致します。