I'm new to WCF and unfortunately only know VB.NET. Having read this post - DataContract XML serialization and XML attributes which is most interesting I have attempted to directly convert it to VB.NET, but it does not seem to give me the right results. All I get is the root element with no attributes:
<root xmlns="http://schemas.datacontract.org/2004/07/shutlBookingService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>;
Any clues on how to add attributes to an element using VB.NET will be greatly appreciated.
OK Here's the code:
<ServiceContract()>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerCall)>
Public Class Service1
<WebGet(UriTemplate:="Test", ResponseFormat:=WebMessageFormat.Xml)>
Public Function Test() As Test
Dim mc As New Test
mc.timestamp = Now
mc.success = "0"
Return mc
End Function
End Class
And the Data Contract is:
Imports System
Imports System.Runtime.Serialization
Imports System.Xml
Imports System.Xml.Serialization
Imports System.ServiceModel
<DataContract()> _
<XmlSerializerFormat()> _
Public Class Test
Private timestamp_att As Date
Private success_att As String = ""
<DataMember()>
<XmlAttribute(AttributeName:="timestamp")> _
Public Property timestamp() As Date
Get
Return Me.timestamp_att
End Get
Set(value As Date)
Me.timestamp_att = value
End Set
End Property
<DataMember()>
<Xml.Serialization.XmlAttribute(AttributeName:="success")> _
Public Property success() As String
Get
Return Me.success_att
End Get
Set(value As String)
Me.success_att = value
End Set
End Property
End Class
The output is:
<Test xmlns="http://schemas.datacontract.org/2004/07/shutlBookingService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<success>0</success>
<timestamp>2011-10-22T09:28:52.7884244+01:00</timestamp>
</Test>
Which as you can see does not have success and timestamp as attributes.