最近在處理有關XML的程式時,在想這是在替客戶做客製化程式時很好用的東西
理由就是程式方面的彈性以及可以清楚查看的順暢度
雖然也有config或是以Text檔案可以使用
但個人比較喜歡用XML去做設定
那麼廢話不多說,以下就介紹基本的建立與讀取XML的功能
Imports System.Xml
'建立XML
Private Sub CreateXML()
Dim xdoc As XmlDocument
Dim xElement As XmlElement
Dim xChildElement As XmlElement
Dim xElement2 As XmlElement
Dim xChildElement2 As XmlElement
Try
'建立一個 XmlDocument 物件並加入 Declaration
xdoc = New XmlDocument
xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes"))
'建立根節點物件並加入 XmlDocument 中 (第0層)
xElement = xdoc.CreateElement("sections")
'在sections寫入一個屬性
xElement.SetAttribute("data_name", "部門人員資訊")
xdoc.AppendChild(xElement)
'在sections下寫入一個節點名稱為section(第1層)
xChildElement = xdoc.CreateElement("section")
xChildElement.SetAttribute("department", "人資部")
xChildElement.SetAttribute("department_code", "2200")
xElement.AppendChild(xChildElement)
'第2層節點
xElement2 = xdoc.CreateElement("users")
xChildElement.AppendChild(xElement2)
'第3層節點
xChildElement2 = xdoc.CreateElement("user")
xChildElement2.SetAttribute("Name", "孫小美")
xChildElement2.SetAttribute("Year", "16")
xChildElement2.SetAttribute("Sex", "女")
xElement2.AppendChild(xChildElement2)
'在sections下寫入一個節點名稱為section(第1層)
xChildElement = xdoc.CreateElement("section")
xChildElement.SetAttribute("department", "資訊部")
xChildElement.SetAttribute("department_code", "3100")
xElement.AppendChild(xChildElement)
'第2層節點
xElement2 = xdoc.CreateElement("users")
xChildElement.AppendChild(xElement2)
'第3層節點
xChildElement2 = xdoc.CreateElement("user")
xChildElement2.SetAttribute("Name", "張寶成")
xChildElement2.SetAttribute("Year", "23")
xChildElement2.SetAttribute("Sex", "男")
xElement2.AppendChild(xChildElement2)
xChildElement2 = xdoc.CreateElement("user")
xChildElement2.SetAttribute("Name", "蕭瀟瀟")
xChildElement2.SetAttribute("Year", "26")
xChildElement2.SetAttribute("Sex", "男")
xElement2.AppendChild(xChildElement2)
xdoc.Save("C:\Users\Desktop\XML_Create.xml")
Catch ex As Exception
MessageBox.Show(ex.Message & System.Environment.NewLine & ex.StackTrace)
End Try
End Sub
其中XML的內容在生成後長成這樣
==================================================
<?xml version="1.0"encoding="UTF-8"standalone="yes"?>
<sections data_name="部門人員資訊">
<section department="人資部"department_code="2200">
<users>
<user Name="孫小美" Year="16"Sex="女" />
</users>
</section>
<section department="資訊部"department_code="3100">
<users>
<user Name="張寶成"Year="23"Sex="男" />
<user Name="蕭瀟瀟"Year="26"Sex="男" />
</users>
</section>
</sections>
==================================================
'讀取XML
Private Sub LoadXML()
Dim xdoc As XmlDocument = New XmlDocument
Dim xRoot As XmlNode
Dim xNodeList As XmlNodeList
Dim xNodeTemp As XmlNode
Dim xUsersNode As XmlNode
Dim xUserNodeList As XmlNodeList
Dim xUserNode As XmlNode
Dim xElement As XmlElement
Try
'讀取XML
xdoc.Load("C:\Users\Desktop\XML_Create.xml")
xRoot = CType(xdoc.DocumentElement, XmlNode)
'選擇section
xNodeList = xRoot.SelectNodes("section[@department!='' and @department_code='3100']")
For intI As Integer = 0 To xNodeList.Count - 1
xNodeTemp = xNodeList.Item(intI)
'取得節點[users]
xUsersNode = xNodeTemp.SelectSingleNode("users")
'取得[user]Name有值的節點
xUserNodeList = xUsersNode.SelectNodes("user[@Name!='']")
For intJ As Integer = 0 To xUserNodeList.Count - 1
xUserNode = xUserNodeList.Item(intJ)
xElement = CType(xUserNode, XmlElement)
Console.Write("姓名:" & xElement.GetAttribute("Name"))
Console.WriteLine()
Console.Write("年齡:" & xElement.GetAttribute("Year"))
Console.WriteLine()
Console.Write("性別:" & xElement.GetAttribute("Sex"))
Console.WriteLine()
Next
Next
Catch ex As Exception
MessageBox.Show(ex.Message & System.Environment.NewLine & ex.StackTrace)
End Try
End Sub
最後得到的結果如下
==================================================
姓名:張寶成
年齡:23
性別:男
姓名:蕭瀟瀟
年齡:26
性別:男
==================================================
是不是很簡單呢?希望一起寫程式的同伴可以一起努力一起成長下去
~不怕新出現的敵人,只怕沒有一起競爭的對手與同伴向高處努力爬~
留言列表