01 Imports System.Reflection
11
12 Public Function getXElementFromLinqObject(ByRef RootElement As XElement, _
13 ByVal DataObject As Object) As XElement
14
15 If RootElement Is Nothing Then RootElement = New XElement("root")
16
17 Dim PropertyInfo As PropertyInfo() = DataObject.GetType.GetProperties()
18
19 For Each PropertyItem As PropertyInfo In PropertyInfo
20 Try
21
22 Dim PropertyValue As Object = PropertyItem.GetValue(DataObject, Nothing)
23
24 If PropertyValue Is Nothing Then
25 Dim x As New XElement(PropertyItem.Name, PropertyValue)
26 RootElement.Add(x)
27 Else
28 Dim currentTypeString As String = PropertyValue.GetType.ToString
29
30 If currentTypeString.StartsWith("System.Data.Linq.EntitySet") Then
31 '
32 ElseIf Not PropertyValue Is Nothing _
33 AndAlso (DirectCast(PropertyValue.GetType, _
34 System.Type).Namespace Is Nothing) Then
35
36 Dim x As New XElement(PropertyItem.Name)
37 RootElement.Add(x)
38 getXElementFromLinqObject(x, PropertyValue)
39
40 ElseIf DirectCast(PropertyValue.GetType, _
41 System.Type).AssemblyQualifiedName.Contains( _
42 "System.Linq.Enumerable") Then
43
44 Dim x As New XElement(PropertyItem.Name & "_List")
45 RootElement.Add(x)
46
47 For Each e In PropertyValue.GetEnumerator()
48
49 Dim x1 As New XElement(PropertyItem.Name)
50 getXElementFromLinqObject(x1, e)
51 x.Add(x1)
52
53 Next
54
55
56
57 ElseIf DirectCast(PropertyValue.GetType, System.Type).GetType.Name _
58 = "RuntimeType" Then
59
60 Dim x As New XElement(PropertyItem.Name, PropertyValue)
61 RootElement.Add(x)
62
63
64
65 Else
66
67 Dim x As New XElement(PropertyItem.Name, PropertyValue)
68 RootElement.Add(x)
69
70 End If
71
72 End If
73 Catch ex As Exception
74
75 End Try
76 Next
77
78 Return RootElement
79 End Function