Welcome to CrankyGoblin.Com Sign in | Join | Help

Public Class GeoffAppleby

Inherits Microsoft.VisualBasic.MVP : Implements IBrainFart
Two features of the XmlSerializer that I never knew

Last night, while playing with a new version of a .Text->CommunityServer converter that had come out, I had some time to kill while it ran, so I had a read of some recent posts on the aus-dotnet mailing list.

 There was some discussion about the XmlSerializer, and someone wanting to only serialize a property at certain times. He'd tried one way, which I never knew about, and then someone else chimed in with a different way. I thought I'd describe both these ways, so that people might become more aware of them (and so I have a place to look it up again :)

To be fair to my easily bruised ego, I won't say that I never could have figured this out, it's just that I've never needed to only sometimes serialize a property, so I never needed to research it. Honest!

The REALLY Simple Way

This one completely blew me away, as it's so damn simple. It's documented in the MSDN library, but so embedded far enough down the page (without sub-headings) that it's easy to skip over.

So, first let's say we have a class Foo, which has a property Bar.

Public Class Foo

  Private msBar As String

  Public Property Bar() As String
    Get
      Return msBar
    End Get
    Set(ByVal Value As String)
      msBar = Value
    End Set
  End Property

End Class

Sometimes we want Bar serialized, sometimes we don't. So we add a property called BarSpecified which returns a boolean. If BarSpecified returns True, then Bar is serialized. If it returns False, then Bar isn't.

Imports System.Xml.Serialization

Public Class Foo

  Private msBar As String
  Private mbBarSpecified As Boolean

  Public Property Bar() As String
    Get
      Return msBar
    End Get
    Set(ByVal Value As String)
      msBar = Value
    End Set
  End Property

  <XmlIgnore()> _
  Public Property BarSpecified() As Boolean
    Get
      Return mbBarSpecified
    End Get
    Set(ByVal Value As Boolean)
      mbBarSpecified = Value
    End Set
  End Property

End Class

Also, note that BarSpecified is itself marked with an XmlIgnoreAttribute. We don't need to serialize this value itself.

The Less Easy Way

This way requires more work, but gives a lot more flexibility. Here's a sample.

Imports System.Xml.Serialization

Public Class Foo

  Private msBar As String
  Private moSerializer As XmlSerializer

  Public Property Bar() As String
    Get
      Return msBar
    End Get
    Set(ByVal Value As String)
      msBar = Value
    End Set
  End Property

  Public Sub CreateSerializer()
    Dim oOverrides As New XmlAttributeOverrides
    Dim oAttributes As New XmlAttributes

    oAttributes.XmlIgnore = True
    oOverrides.Add(Me.GetType, "Bar", oAttributes)

    moSerializer = New XmlSerializer(Me.GetType, oOverrides)
  End Sub

End Class

Here we create an XmlAttributes object, and tell it that it's ignoring. We add that to an XmlAttributeOverrides object, specifying that the attribute gets applied to any object that matched Me.GetType (so, Class Foo), and only to apply it to a property or field named Bar. Then this XmlAttributeOverrides object gets passed to the XmlSerializer constructor.

Anything defined in the XmlAttributesOverrides object completely overrides anything else set on the property, so be careful when you mix and match :)

The real thing to be careful of is that reconstruction on deserialization might depend on any overrides you set when serializing - a good example of this is if you set a XmlElementAttribute in the overrides to change the name of the XML node.When deserializing you'll get an error if you don't override it at the other end too.

A Last Quick Tip

Oh, and one more thing. When you stop and think about it, it's obvious, but when it first bites you it's a surprise. Please try and remember that ReadOnly or WriteOnly properties do not and will not ever get serialized :)

Listening to: endless - def fx - (2:25)
Posted: Wednesday, March 09, 2005 1:10 PM by Geoff Appleby
Filed under:

Comments

RIO - Randektív Informatikai Oldal said:

&lt;p&gt;&amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;http://story.news.yahoo.com/news?tmpl=story&amp;amp;cid=562&amp;amp;ncid=738&amp;amp;e=6&amp;amp;u=/ap/20050323/ap_on_hi_te/yahoo_upgrades&amp;quot; target=&amp;quot;_blank&amp;quot;&amp;gt;Yahoo is 1Gb-re emeli a mail-t&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li
# March 23, 2005 7:07 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

To submit your comment, click on these pictures:
  • Geoff's little sister's pussy
  • Sleepy Geoff
  • Happy Geoff
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Can't recognise the people in these pictures? Look here for a quick introduction.
There's a time limit for you to get your comment submitted before this set of pictures expires. If you think it's been longer than 10 minutes, get some new pictures first (you won't lose what you've typed so far).
Get some new pictures 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS