Public Sub SetDefaultValues(ByVal targetObject As Object)
'If the filename isn't set exit the method
If Me.FileName.Trim.Length = 0 Then Exit Sub
'Collect the property descriptors for the object
Dim propDescriptors As System.ComponentModel.PropertyDescriptorCollection
propDescriptors = System.ComponentModel.TypeDescriptor.GetProperties( _
targetObject.GetType)
'Read the xml file into an xml document
Dim xmlDoc As New System.Xml.XmlDocument
xmlDoc.Load(Me.FullDataPath)
'Create a namespace manager
Dim nmspMgr As New Xml.XmlNamespaceManager(xmlDoc.NameTable)
'Loop through all of the children of the xml document and collect the
'namespaces into the namespace manager
Dim rootChild As System.Xml.XmlNode
For Each rootChild In xmlDoc.ChildNodes
Dim childAttribute As System.Xml.XmlAttribute
If Not rootChild.Attributes Is Nothing Then
For Each childAttribute In rootChild.Attributes
If childAttribute.Name.StartsWith("xmlns") Then
If childAttribute.Name = "xmlns" Then
nmspMgr.AddNamespace("", childAttribute.InnerText)
Else
'Extract the prefix
Dim prefix As String
prefix = Mid(childAttribute.Name, 7)
nmspMgr.AddNamespace(prefix, childAttribute.InnerText)
End If
End If
Next
End If
Next
'Collect the the namespace prefix that matches the XMLDataNamespace
Dim nsPrefix As String
nsPrefix = nmspMgr.LookupPrefix(My.Settings.XMLDataNamespace)
'If the prefix is not empty add a colon to the end of it
If nsPrefix.Trim.Length > 0 Then nsPrefix += ":"
'Collect the default value nodes
Dim defaultNodeList As System.Xml.XmlNodeList
defaultNodeList = xmlDoc.SelectNodes("//" & nsPrefix & "DefaultValue", nmspMgr)
'Loop through each node in the list setting the property value
Dim currentNode As System.Xml.XmlElement
For Each currentNode In defaultNodeList
'Collect the child nodes into two variables
Dim propertyName As String = ""
Dim defaultValue As Object = Nothing
Dim childNode As System.Xml.XmlElement
For Each childNode In currentNode.ChildNodes
Select Case childNode.Name
Case Is = nsPrefix & "Property"
propertyName = childNode.InnerText
Case Is = nsPrefix & "Value"
defaultValue = childNode.InnerText
End Select
Next
'Collect the property descriptor
Dim currentProperty As System.ComponentModel.PropertyDescriptor
currentProperty = propDescriptors.Find(propertyName, True)
'Set the value
If Not currentProperty Is Nothing Then
Dim propType As System.Type = currentProperty.GetType
Select Case currentProperty.PropertyType.FullName
Case Is = "System.Decimal"
defaultValue = System.Convert.ToDecimal(defaultValue)
Case Is = "System.String"
defaultValue = System.Convert.ToString(defaultValue)
Case Is = "System.Int16"
defaultValue = System.Convert.ToInt16(defaultValue)
Case Is = "System.Int32"
defaultValue = System.Convert.ToInt32(defaultValue)
Case Is = "System.Int64"
defaultValue = System.Convert.ToInt64(defaultValue)
Case Is = "System.Byte"
defaultValue = System.Convert.ToByte(defaultValue)
Case Is = "System.Date"
defaultValue = System.Convert.ToDateTime(defaultValue)
End Select
currentProperty.SetValue(targetObject, defaultValue)
End If
Next
End Sub