all groups > dotnet windows forms designtime > january 2007 >
You're in the

dotnet windows forms designtime

group:

How to save the Form Controls to an XML file


How to save the Form Controls to an XML file Carlos Rodriguez
1/3/2007 8:27:20 PM
dotnet windows forms designtime:
Hello Can someone tell me how to save the controls and its properties on a
form to an xml file

I have problems when it come to the Font, Color, Cursor ... properties

heres`s an ex of what I do:

Private Sub SaveCtrlsProp()

Dim cCtrl As Control

Dim cCtrlProperty As PropertyCollection

Dim tType As Type

Dim miMemberInfo As MemberInfo

For Each cCtrl In Me.Controls

Dim props() As PropertyInfo = cCtrl.GetType.GetProperties

Dim j As Integer = 0

Do While (j < props.Length)

Dim membInfo As MemberInfo = props(j).PropertyType

If (props(j).CanWrite AndAlso (props(j).CanRead AndAlso
(props(j).Name <> "ReadOnly"))) Then

moXML.SaveSetting(cCtrl.GetType.Name, props(j).Name(),
props(j).GetValue(cCtrl, Nothing))

End If

j += 1

Loop

Next

End Sub

' ******************************************************************

Private Sub SaveSetting(ByVal ObjectName As String, ByVal PropertyName As
String, ByVal PropertyValue As String)

Dim xnObject As XmlNode

Dim xnProperty As XmlNode

Dim xnAttr As XmlAttribute

'check the document exists, create if not

If m_xmlDocument.DocumentElement Is Nothing Then

Try

m_xmlDocument.LoadXml("<?xml version=""1.0"" encoding=""UTF-8""?>" &
ControlChars.CrLf & _

"<FormSettings>" & ControlChars.CrLf & "</FormSettings>")

Catch e1 As Exception

Debug.WriteLine("_SaveSetting - Error creating Document: " & e1.ToString)

End Try

End If

xnObject = m_xmlDocument.SelectSingleNode("//Object[@Name='" & ObjectName &
"']")

'check the Object exists, create if not

If xnObject Is Nothing Then

Try

'create the new Object node...

xnObject = m_xmlDocument.CreateNode(XmlNodeType.Element, "Object", "")

'add the Name attribute

xnAttr = m_xmlDocument.CreateAttribute("Name")

xnAttr.Value = ObjectName

xnObject.Attributes.SetNamedItem(xnAttr)

'get the root XML node and add the new node to the document

Dim xnRoot As XmlNode = m_xmlDocument.DocumentElement

xnRoot.AppendChild(xnObject)

xnRoot = Nothing

Catch e2 As Exception

Debug.WriteLine("_SaveSetting - Error creating Object: " & e2.ToString)

End Try

End If

xnProperty = xnObject.SelectSingleNode("descendant::Property[@Name='" &
PropertyName & "']")

'check the Property exists, create if not

If xnProperty Is Nothing Then

Try

'create the new Property node...

xnProperty = m_xmlDocument.CreateNode(XmlNodeType.Element, "Property", "")

'add the Name attribute

xnAttr = m_xmlDocument.CreateAttribute("Name")

xnAttr.Value = PropertyName

xnProperty.Attributes.SetNamedItem(xnAttr)

'add the Value attribute

xnAttr = m_xmlDocument.CreateAttribute("Value")

xnAttr.Value = PropertyValue

xnProperty.Attributes.SetNamedItem(xnAttr)

'add the new node to its Object

xnObject.AppendChild(xnProperty)

Catch e3 As Exception

Debug.WriteLine("_SaveSetting - Error creating Property: " & e3.ToString)

End Try

Else

xnProperty.Attributes("Value").Value = PropertyValue

End If

'save changes

Try

m_xmlDocument.Save(XMLFileName)

Catch e4 As Exception

Debug.WriteLine("_SaveSetting - Error Saving File: " & e4.ToString)

End Try

xnProperty = Nothing

xnObject = Nothing

End Sub



Re: How to save the Form Controls to an XML file Carlos Rodriguez
1/4/2007 11:00:16 AM
I`va found a very nice solution here:
http://msdn2.microsoft.com/en-us/library/ms973818.aspx


"Carlos Rodriguez" <carlosrturcios@hotmail.com> a écrit dans le message de
news: ODaWs95LHHA.4376@TK2MSFTNGP03.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button