عند تحميل النموذج يقوم البرنامج بإنشاء كائن وثيقة إكس ام ال XmlDocument يحتوعي على البيانات الموجودة في ملف XML وهي تستخدم الاجراء GetAttributesAndNames للحصول على قائمة بالخصائص والعناصر في الشجرة الفرعية لعقدة الموظفين الأولى ويقوم بتقسيم قوائم الأسماء باستخدام رؤوس الأعمدة الخاصة بـ ListView.
ثم يقوم البرنامج بالدوران خلال عناصر ملف الموظفين وهو يستدعي الإجراء GetAttributeAndNameValues ليحصل على خصائص وأسماء القيم المطابقة لأسماء الأعمدة ومن ثم إضافتهم لـ ListView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Compose the XML file's name.
Dim file_name As String
file_name = Application.StartupPath
If file_name.EndsWith("\bin") Then
file_name = file_name.Substring(0, _
file_name.LastIndexOf("\bin"))
End If
file_name &= "\Employees.xml"
' Load the XML file.
Dim xml_doc As New XmlDocument
xml_doc.Load(file_name)
Dim root_node As XmlElement = xml_doc.DocumentElement
' Use the first Employee node to make the column
' headers.
Dim employee_node As XmlNode = root_node.FirstChild
Dim txt As String = GetAttributesAndNames(employee_node)
Dim values() As String = txt.Split("|"c)
For Each str As String In values
Dim col_header As New ColumnHeader
col_header.Text = str
col_header.Width = 100
lvwEmployees.Columns.Add(col_header)
Next str
' Read the Employee values.
For Each employee_node In root_node.ChildNodes
txt = GetAttributeAndNameValues(employee_node)
values = txt.Split("|"c)
If values.Length > 0 Then
Dim lvw_item As ListViewItem = _
lvwEmployees.Items.Add(values(0))
For i As Integer = 1 To values.Length - 1
lvw_item.SubItems.Add(values(i))
Next i
End If
Next employee_node
' Size the columns to fit.
For Each hdr As ColumnHeader In lvwEmployees.Columns
hdr.Width = -2
Next hdr
End Subالإجراء GetAttributesAndNames يعيد الخصائص والعناصر من أجل الشجرة الفرعية لعقدة وهو أولا يدور خلال خصائص العقدة مضيفا أسماءهم لنص ثم يدور خلال العقد الأبناء
وإذا كانت العقدة تحتوي على عقدة نص وحيدة فتكون العقدة الابن هي بمستوى ورقة فيقوم البرنامج بإضافتها للنتائج
وإن لم تحتوي على نص وحيد فالبرنامج يدور خلال العناصر الأبناء مستدعيا الإجراء GetAttributesAndNames بشكل متكرر ويقوم بإضافة النتائج
' Return a string giving the element names and attributes ' for this node's subtree separated by | characters. Private Function GetAttributesAndNames(ByVal xml_node As _ XmlNode) As String Dim result As String = "" ' Get this element's attributes. For Each attr As XmlAttribute In xml_node.Attributes result &= "|" & xml_node.Name & " " & attr.Name Next attr ' If the element has a single text child, ' add the element's name. If (xml_node.ChildNodes.Count = 1) And _ (xml_node.FirstChild.NodeType = XmlNodeType.Text) _ Then ' It's a text node. Use its name. result = result & "|" & xml_node.Name Else ' Get the sub-tree's names and attributes. For Each child As XmlNode In xml_node.ChildNodes If child.NodeType = XmlNodeType.Element Then result = result & "|" & _ GetAttributesAndNames(child) End If Next child End If If Len(result) > 0 Then result = result.Substring(1) Return result End Function
الإجراء GetAttributeAndNameValues يعمل تقريبا كالاجراء GetAttributesAndNames ونتوقع منه إعادة قيم الخصائص و العناصر النصية بالإضافة لأسمائها
' Return a string giving the values of element names and ' attributes ' for this node's subtree separated by | characters. Private Function GetAttributeAndNameValues(ByVal xml_node _ As XmlNode) As String Dim result As String = "" ' Get this element's attribute values. For Each attr As XmlAttribute In xml_node.Attributes result &= "|" & attr.Value Next attr ' If the element has a single text child, ' add the element's name. If (xml_node.ChildNodes.Count = 1) And _ (xml_node.FirstChild.NodeType = XmlNodeType.Text) _ Then ' It's a text node. Use its value. result = result & "|" & _ xml_node.FirstChild.InnerText Else ' Get the sub-tree's names and attributes. For Each child As XmlNode In xml_node.ChildNodes If child.NodeType = XmlNodeType.Element Then result &= "|" & _ GetAttributeAndNameValues(child) End If Next child End If If Len(result) > 0 Then result = result.Substring(1) Return result End Function
مترجم