Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / January 2007

Tip: Looking for answers? Try searching our database.

How to save the Form Controls to an XML file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlos Rodriguez - 04 Jan 2007 01:27 GMT
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
Carlos Rodriguez - 04 Jan 2007 16:00 GMT
I`va found a very nice solution here:
http://msdn2.microsoft.com/en-us/library/ms973818.aspx

> Hello Can someone tell me how to save the controls and its properties on a
> form to an xml file
[quoted text clipped - 168 lines]
>
> End Sub

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.