views:

40

answers:

1

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.

+2  A: 

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
I have another property Phones that is a List<string>, can I make the serialized xml strings to be <Phone>5555</Phone> rather than <string>5555</string>, or even <phone number="5555" />
Shimmy
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
BTW, is your company recruiting by any chance? :-)
M.A. Hanin
'your company' lol! when will that be? let's create one...
Shimmy
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
Yes, contact me.Anyway, regarding the question I used the `XmlArrayItem`, it works for me. Thanks
Shimmy