Search code examples
xml-serializationxmlserializerixmlserializablexml-attribute

Is there a way to make a serialized member to serialize as an attribute?


Is there a way to make a serialized member to serialize as an attribute:

<Serializable> 
Public Class Person
    Public Property Name As String
End Class

I want than when this class is xml-serialized, it should produce:

<Person Name="John Doe" />

And what I mean is that instead of the Name property should be serialized as an element, it should be serialized as an xml attribute.


Solution

  • I think you're looking for the XmlAttribute attribute:

    <Serializable()> 
    Public Class Person
        <Xml.Serialization.XmlAttribute()> 
        Public Property Name() As String
    End Class
    

    See more details and Xml-serialization attributes here.