2

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.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

1 Answers1

2

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.

M.A. Hanin
  • 8,044
  • 33
  • 51
  • I have another property Phones that is a List, can I make the serialized xml strings to be 5555 rather than 5555, or even – Shimmy Weitzhandler Apr 22 '10 at 07:17
  • I think you can, using the XmlElement attribute with the ElementName argument set to Phone. Open a new SO question if you don't succeed. – M.A. Hanin Apr 22 '10 at 07:38
  • BTW, is your company recruiting by any chance? :-) – M.A. Hanin Apr 22 '10 at 07:39
  • For some reason I thought you're employed at some software company. I'm a freelancer at the moment (about 60 minutes away from you :-) ). Anyhow, if you're willing to privately discuss some stuff, I'll contact you. – M.A. Hanin Apr 22 '10 at 09:59
  • Yes, contact me. Anyway, regarding the question I used the `XmlArrayItem`, it works for me. Thanks – Shimmy Weitzhandler Apr 22 '10 at 12:15