I am currently working on an integration with an external company and the interactions are done using objects serialized into XML strings. The examples that I've been given and the responses from the external service include XML tags with properties inside the tags themselves.
i.e.:
<?xml version="1.0" encoding="UTF-8"?>
<Response Version="3">
<Status StatusCode="OK"></Status>
</Response>
Orignially I was expecting "OK" to be the value between the "Status" tags so that it would be deserialized as a string value on the resulting object but the "Status" field is ending up as an empty string, I'm assuming because the tag is technically empty.
I'm currently trying to use a DataContractSerializer
due to it being the best fit given some other separate requirements but I am unsure how to handle tags like the "Status" example.
Is a DataContractSerializer
capable of deserializing the "Status" tag in its current form? If not, what type of serializer should be used to accomodate this scenario?
Also, any resources or help with search terms for what this XML structure is called would be appreciated.
You can serialize to these classes
Public Class Response
<System.Xml.Serialization.XmlElement()>
Public Property Status As Status
<System.Xml.Serialization.XmlAttribute()>
Public Property Version As Integer
End Class
Public Class Status
<System.Xml.Serialization.XmlAttribute()>
Public Property StatusCode As String
End Class
(Version
and StatusCode
are Attributes)
Here is the serialization
Dim s As New Xml.Serialization.XmlSerializer(GetType(Response))
Dim r As Response
' reading
Using fs As New IO.FileStream("filename.xml", IO.FileMode.Open)
r = s.Deserialize(fs)
End Using
' writing
Using fs As New IO.FileStream("filename.xml", IO.FileMode.Truncate)
s.Serialize(fs, r)
End Using
If it helps, the xml could also be represented like this, which should make it clear why the Status
element itself has no string value.
<?xml version="1.0" encoding="UTF-8"?>
<Response Version="3">
<Status StatusCode="OK"/>
</Response>