1

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.

vimuth
  • 5,064
  • 33
  • 79
  • 116
Tim
  • 31
  • 1
  • 5
  • I don't see any elements in your DataContract class - perhaps it needs a default element to hang attributes on? I'm not really familiar with XmlSerializer, so I could be wrong. If you have time you might want to add some property or field and set the XmlText attribute and see if that helps any. – Tim Oct 22 '11 at 08:41

2 Answers2

1

You didn't post your code, but you may have used the wrong syntax - C# uses square brackets ([, ]), VB.NET uses angle brackets (<, >):

Example (based on the C# code in the linked answer, changed to VB.NET by hand so there may be some minor syntactic typos):

<DataContract>
<XmlSerializerFormat>
Public Class root

    Dim distance As New Distance();

End Class

<DataContract>
Public Class distance

    <DataMember>
    <XmlAttribute>
    Pubilc units As String = "m"

    <DataMember>
    <XmlText>
    Public value As Integer = 1000

End Class

You'll also need to add a reference to System.Runtime.Serialiation and System.Xml.Serialization:

Imports System.Runtime.Serialization
Imports System.Xml.Serialzation

UPDATE To follow up to my suggestion in my comment under your question, what if you did something like this:

<DataContract()> _
<XmlSerializerFormat()> _
Public Class Test

    Private timestamp_att As Date
    Private success_att As String = ""

    'Set up an element
    <DataMember()>
    <XmlElement(ElementName:="Data")> _
    Public DataElement 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 expected output should be something like this (I haven't tested this):

<Test xmlns="http://schemas.datacontract.org/2004/07/shutlBookingService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Data success="0" timestamp="2011-10-22T09:28:52.7884244+01:00" /></Test>
Tim
  • 28,212
  • 8
  • 63
  • 76
  • Sorry, but I can't get this to work. See below where I have posted full code. – Tim Oct 22 '11 at 08:20
  • I don't see any code - it's probably better to update (edit) your question and post the code there. – Tim Oct 22 '11 at 08:22
  • Im a new user on this site - and finding it very good - and have altered it and put the code in my original post. – Tim Oct 22 '11 at 08:33
  • Welcome to SO - this is a great community. I looked at your added code and asked a question above. – Tim Oct 22 '11 at 08:41
0

I was working on the same thing a few weeks back here is a sample of the code I got to work, hope it helps (i've included the xml I used to test this):

        Public Function test()

            Dim output As StringBuilder = New StringBuilder()

            Dim xmlString As String = _
                "<bookstore>" & _
                        "<book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'>" & _
                            "<title>The Autobiography of Benjamin Franklin</title>" & _
                            "<author>" & _
                                "<first-name>Benjamin</first-name>" & _
                                "<last-name>Franklin</last-name>" & _
                            "</author> " & _
                            "<price>8.99</price>" & _
                        "</book>" & _
                    "</bookstore>"



            Dim s As XNamespace = "TestNamespace"
            Dim e As XElement = XElement.Parse(xmlString)
            Dim price = e.Element("inventory").Element("price").Value


            'OutputTextBlock.Text = output.ToString()
        End Function
Standage
  • 1,517
  • 7
  • 22
  • 41