Export object properties and attributes to XML

Hey,
Im wondering if there is some way to export all object properties and
attributes such as size, position, color, etc in a VB project/file to XML? I
am using Visual Studio .net 2003.
Thanks.
[272 byte] By [JustinL] at [2007-12-16]
# 1
Ultimately you could use XmlSerialization and Xsd.exe to produce what you are looking for. However, i'm guessing that this isn't what you want because "everything" wont neccessarily be output with those methods unless you do quite a bit of work ahead of time.

You can always use reflection to look at an assembly like the following:



Imports
System.Reflection
Imports System.Xml

Module
Module1
Dim methodElement As XmlElement
Dim paramElement As XmlElement
Dim templateDoc As New XmlDocument

Sub
Main()
templateDoc.LoadXml(
"<program/>")
Dim my_Type As System.Type = GetType(MySpecialClass)
Dim methods As MethodInfo() = my_Type.GetMethods()
For Each method As MethodInfo In methods
AppendMethod(method.Name, _
IIf(method.IsPrivate,
"private", _
IIf(method.IsPublic,
"public", "")), method.GetParameters())
Next
Console.Out.WriteLine(templateDoc.InnerXml)
Console.In.ReadLine()
End Sub

Sub
AppendMethod(ByVal name As String, _
ByVal modifier As String, _
Optional ByVal params As _
ParameterInfo() =
Nothing)
methodElement = templateDoc.CreateElement(
"method")
methodElement.Attributes.Append( _
templateDoc.CreateAttribute(
"modifier")).Value = modifier
methodElement.Attributes.Append( _
templateDoc.CreateAttribute(
"name")).Value = name
If Not IsNothing(params) Then
For Each param As ParameterInfo In params
paramElement = templateDoc.CreateElement(
"param")
paramElement.Attributes.Append( _
templateDoc.CreateAttribute(
"name")).Value = param.Name
methodElement.AppendChild(paramElement)
Next
End If
templateDoc.DocumentElement.AppendChild(methodElement)
End Sub
End
Module

Public Class MySpecialClass
Private my_Property As String
Public Property MyProperty() As String
Get
Return my_Property
End Get
Set(ByVal value As String)
my_Property = value
End Set
End Property
Private Function MyPrivateFunction(ByVal myParam As String) As String
Return String.Empty
End Function
Public Function MyPublicFunction(ByRef myParam As String) As String
Return String.Empty
End Function
End
Class



This example simply looks at the methods in an assembly and writes them to XmlElements. I'm sure theres a better way to produce the output, but you get the idea.

If you were just looking for the publicly exposed properties

TobinTitus at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic General...