الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف نقوم بتعبئة ListView من ملف XML في فيجول بايزيك دوت نيت

مغلق
بدأه محمد سامر أبو سلو في 12 أغسطس 2006 · 3 رد · 647 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

عند تحميل النموذج يقوم البرنامج بإنشاء كائن وثيقة إكس ام ال 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

مترجم

howto_net_load_listview_from_xml.zip

عن عائشة رضي الله عنها أن النبي صلى الله عليه وسلم قال: إن الله يحب إذا عمل أحدكم عملا أن يتقنه

نعيب زماننا والعيب فينا ... وما لزماننا عيب سوانا

ونهجو ذا الزمان بغير ذنب ... ولو نطق الزمان لنا هجانا

محمد سامر أبو سلو

#2

جزاكم الله خيرا أخي محمد

Up

وياريتك كنت كتبت موضوع تافه علشان تلاقي تفاعل من الأعضاء :(

#3

لو كان الموضوع يتحدث عن تجميل للواجهات لأجتذب كثيرا من الاعضاء

هناك الكثير ممن تهمهم القشور فيتباهون بواجهات برامجهم و عدد السطور التي كتبوهوا

لكنهم بعيدون كل البعد عن البرمجة فواجهاتهم تخفي برامج لا تعمل و سطورهم غثاء كغثاء السيل

اللهم صل و سلم على سيدنا محمد

كاف و نون خلقهم و فنائهم كاف و نون

عالمي شاشة و لغتي صفر و واحد

Just Smile ............You can do it

When you stop learning ... You stop leading

لساني بنطقي صامت عنه عادل............وقلبي بصمتي ضاحك منه هازل

وأتـعـب من نـاداك من لا تـجيبـه............وأغـيظ من عـاداك من لا تـشاكـل

مدونتي الشخصية : Tammam Koujan

آخر مواضيع المدونة :

#4
hazzoom كتب:
جزاكم الله خيرا أخي محمد

Up

وياريتك كنت كتبت موضوع تافه علشان تلاقي تفاعل من الأعضاء :(

شكرا لك أخ حزوم على الرد الجميل - ويكفيني سعادة أنك توقفت لقراءة الموضوع

Tammam Koujan كتب:
لو كان الموضوع يتحدث عن تجميل للواجهات لأجتذب كثيرا من الاعضاء

هناك الكثير ممن تهمهم القشور فيتباهون بواجهات برامجهم و عدد السطور التي كتبوهوا

لكنهم بعيدون كل البعد عن البرمجة فواجهاتهم تخفي برامج لا تعمل و سطورهم غثاء كغثاء السيل

شكرا لك أخ تمام على الرد الجميل

عن عائشة رضي الله عنها أن النبي صلى الله عليه وسلم قال: إن الله يحب إذا عمل أحدكم عملا أن يتقنه

نعيب زماننا والعيب فينا ... وما لزماننا عيب سوانا

ونهجو ذا الزمان بغير ذنب ... ولو نطق الزمان لنا هجانا

محمد سامر أبو سلو

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…