Charles,
I had quite forgotten about the other methods you discovered - have no idea
whether they still function in the same way in 2.0. The
'propertyNameSpecified' method seems much more flexible though.
If you look in the VS2003 help for XmlSerializer class ('about XMLSerializer
class'), about half way down the page is the following (I suspect the MSDN
documentation for this will be identical):
Another option is to use a special pattern to create a Boolean field
recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the
field. The pattern is created in the form of propertyNameSpecified. For
example, if there is a field named "MyFirstName" you would also create a
field named "MyFirstNameSpecified" that instructs the XmlSerializer whether
or not to generate the XML element named "MyFirstName". This is shown in the
following C# and Visual Basic example.
' Visual Basic
Public Class OptionalOrder
' This field's value shouldn't be serialized
' if it is uninitialized.
Public FirstOrder As String
' Use the XmlIgnoreAttribute to ignore the
' special field named "FirstOrderSpecified".
<System.Xml.Serialization.XmlIgnoreAttribute> _
Public FirstOrderSpecified As Boolean
End Class
[quoted text, click to view] "Charles Law" <blank@nowhere.com> wrote in message
news:OT$P2UbJGHA.668@TK2MSFTNGP11.phx.gbl...
> Hi Clive
>
> That does exactly what I want. Thanks.
>
> I have never seen this documented, can you point me to an MSDN description
> of this feature?
>
> Whilst looking myself, I came across another way that seems to work:
> <ComponentModel.DefaultValue("George")> _
> Public Property Name() As String
> Get
> Return m_Name
> End Get
> Set(ByVal Value As String)
> m_Name = Value
> End Set
> End Property
>
> Name now does not get serialized when it returns "George". Also,
> XmlAttributes.XmlDefaultValue can be used to do the same thing at runtime.
> The only concern with this is that MSDN article KB325691 highlights that
> Microsoft intend to change this behaviour in the next major version
> release of the .NET framework. I haven't checked, but I wonder if this
> technique no longer works in .NET 2.0.
>
> Anyway, many thanks again.
>
> Charles
>