G
grwalker
I have some classes that have the <Serializable()> attribute applied,
which of course by default serializes the class properties as elements.
What I would like to do is to be able to override this behavior at
runtime to serialize the properties as attributes.
I have been exploring the XMLAttributesOverrides classes and examples
which claims:
"you can create one set of serializable classes, but serialize the
objects in multiple ways. For example, instead of serializing members
of a class instance as XML elements, you can serialize them as XML
attributes, resulting in a more efficient document to transport."
I have yet to be able to figure out how this can be acomplished
however... For a simple class like the following if I call the
SerializeXML() method I get an error.
<Serializable()> _
Public Class Class3
Public Test As String = "Test"
Private Test2 As String = "Test2"
Private Test3 As String = "Test3"
<XmlIgnore()> _
Public Property Test4() As String
Get
Return Test3
End Get
Set(ByVal Value As String)
Test3 = Value
End Set
End Property
Public Sub SerializeXML(ByRef sw As TextWriter)
Dim attrs As New XmlAttributes
Dim attr As New XmlAttributeAttribute("MyTest",
GetType(String))
attrs.XmlAttribute = attr
Dim attrOverrides As New XmlAttributeOverrides
attrOverrides.Add(Me.GetType, "Test", attrs)
Dim xs As New XmlSerializer(GetType(Class3), attrOverrides)
xs.Serialize(sw, Me)
End Sub
End Class
Can someone PLEASE tell me how I can override the behavior at runtime
to serialize as attributes instead of elelements.
which of course by default serializes the class properties as elements.
What I would like to do is to be able to override this behavior at
runtime to serialize the properties as attributes.
I have been exploring the XMLAttributesOverrides classes and examples
which claims:
"you can create one set of serializable classes, but serialize the
objects in multiple ways. For example, instead of serializing members
of a class instance as XML elements, you can serialize them as XML
attributes, resulting in a more efficient document to transport."
I have yet to be able to figure out how this can be acomplished
however... For a simple class like the following if I call the
SerializeXML() method I get an error.
<Serializable()> _
Public Class Class3
Public Test As String = "Test"
Private Test2 As String = "Test2"
Private Test3 As String = "Test3"
<XmlIgnore()> _
Public Property Test4() As String
Get
Return Test3
End Get
Set(ByVal Value As String)
Test3 = Value
End Set
End Property
Public Sub SerializeXML(ByRef sw As TextWriter)
Dim attrs As New XmlAttributes
Dim attr As New XmlAttributeAttribute("MyTest",
GetType(String))
attrs.XmlAttribute = attr
Dim attrOverrides As New XmlAttributeOverrides
attrOverrides.Add(Me.GetType, "Test", attrs)
Dim xs As New XmlSerializer(GetType(Class3), attrOverrides)
xs.Serialize(sw, Me)
End Sub
End Class
Can someone PLEASE tell me how I can override the behavior at runtime
to serialize as attributes instead of elelements.