Writing control properties to xml

J

John

Hi

I am trying to right all control properties on my form to an xml file. My
complete VB code so far is given at the end below. I have two problems
currently.

1. On the line PropValue = Prop.GetValue(Ctrl) I get the error "Conversion
from type 'Padding' to type 'String' is not valid." when the ctrl is
System.Windows.Forms.ListView and Property is Margin which is of type
Padding and not string. How can I resolve such a situation where property is
not a singel value but a collection of sub values?

2. On line xmlWriter.WriteStartElement(Type) I get the error "Token
StartElement in state Epilog would result in an invalid XML document." when
type is System.Windows.Forms.ListView. How can I resolve such a situation?

Many thanks for your time.

Regards


= Code ==================

Module modUtil

Private xmlWriter As Xml.XmlTextWriter

Public Sub WriteControls(ByVal rootControl As Control)
Dim Prop As PropertyDescriptor
Dim Props As PropertyDescriptorCollection
Dim Ctrl As Control
Dim PropName As String
Dim PropValue As String

WriteXMLStart("c:\contacts_template.xml")

For Each Ctrl In rootControl.Controls
Props = TypeDescriptor.GetProperties(Ctrl)
WriteXMLControlStart(Ctrl, Ctrl.GetType().ToString())
For Each Prop In Props
If (Prop.IsBrowsable) Then
PropName = Prop.Name
PropValue = Prop.GetValue(Ctrl)
WriteXMLProperty(PropValue, PropName)
End If
Next
WriteXMLControlEnd()
Next
WriteXMLEnd()
End Sub

Public Sub WriteXMLStart(ByVal Filename As String)
xmlWriter = New Xml.XmlTextWriter(Filename, System.Text.Encoding.UTF8)
xmlWriter.WriteStartDocument()
End Sub

Public Sub WriteXMLControlStart(ByVal Ctrl As Control, ByVal Type As String)
xmlWriter.WriteStartElement(Type)
xmlWriter.WriteAttributeString("Name", Ctrl.Name)
End Sub

Public Sub WriteXMLProperty(ByVal Value As String, ByVal Name As String)
xmlWriter.WriteStartElement(Name)
xmlWriter.WriteString(Value)
xmlWriter.WriteEndElement()
End Sub

Public Sub WriteXMLControlEnd()
xmlWriter.WriteEndElement()
End Sub

Public Sub WriteXMLEnd()
xmlWriter.WriteEndDocument()
End Sub

End Module
 
M

Marc Gravell

Have you looked at the TypeConverter? i.e.
Prop.Converter.ConvertToInvariantString()? You may also wish to check
CanConvertTo/From

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top