XMLAttribute equivalent for DataContractSerialization?

I'm looking at converting a class that we currently serialize/deserialize with XMLSerializer to use the DataContractSerializer. The main reason I'm looking to switch is because of the OnDeserialized callback functionality you get with the DataContractSerializer. I'd really like to be able to tap into the initialization of an object if I need to. In the class I'm attempting to convert, there are several uses of <XmlAttribute()> and <XmlElement()>, depending on how they show up in the XML.

I've created a test class, just called Person with a few different properties so that I can test out serializing an object to XML and then deserializing it back to an object. That works fine, but the one thing I'm not sure about is what to specify for any property that needs to be an attribute in the XML, not an element ("ID" below).

This is what I want:

<Person ID="5">
  <FullName>FirstName LastName</FullName> 
</Person>

This is what I'm getting:

<Person>
  <FullName>FirstName LastName</FullName>
  <ID>5</ID>
</Person>

In the example below, how to I specify that ID is an attribute and not an element? So the XML when serialized shows up like above:

<DataContract()> _
Public Class Person

    <DataMember()> _
    Public FullName As String

    '//What should go here?
    Public ID As Integer

End Class

Answers


<System.Xml.Serialization.XmlAttribute()>

Should solve your problem, for more information see MSDN XmlAttribute reference.


Need Your Help

How to modify .doc or .docx file in php

php tabs

I have to modify the uploaded .doc or .docx file in php. I googled but i only found how to read that but not as it is.

Postgres unique constraint not deferrable in hibernate

hibernate postgresql unique-constraint

I created an unique constraint in PostgreSQL database and set it to deferrable initially deferred.