2022年8月1日 星期一

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


沒有留言:

張貼留言