Reputation: 61
I am curious if it is possible to create a DataContract that will serialize like so:
<MyCustomName>string value</MyCustomName>
OR
<MyCustomName>int</MyCustomName>
What I am ultimately trying to do is return a basic type with a Custom element name. I've tried the following:
[DataContract(Name = "MyCustomName")]
public class MyCustomName : String
{
}
But obviously you can't inherit a string or int value. Any help would be appreciated.
Upvotes: 4
Views: 136
Reputation: 6017
If you are returning that type as a field in another class, then you could just use a surrogate property such as:
public TreeId Id { get; set; }
[DataMember(Name = "Id")]
private string IdSurrogate
{
get
{
return Id.ToString();
}
set
{
Id = new TreeId(value);
}
}
So instead of having to put DataContract on the TreeId class, the property IdSurrogate just handles translating it directly to a string value
Upvotes: 0
Reputation: 19060
I'm not sure if there is an easier way but as far as I know the DataContractSerializer will respect it if your type implements IXmlSerializable. So you could implement this interface and roll your own serialization of MyCustomName
Upvotes: 2