|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to: Omit Deafulted Elements from XML When Serializingelements that have a default value. For example, if I have a class as follows Public Class Test Private m_Name As String = "George" Private m_Active As Boolean = False Private m_Address As String Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value End Set End Property Public Property Active() As Boolean Get Return m_Active End Get Set(ByVal Value As Boolean) m_Active = Value End Set End Property Public Property Address() As String Get Return m_Address End Get Set(ByVal Value As String) m_Address = Value End Set End Property End Class If Name is "George" when I serialize the object then I don't want this element to be included in the output. Similarly, if Active is "False" when the object is serialized, then I don't want that element included in the output either. Anything different, and they should be included. Is there a way to do this, perhaps by tagging the property/element in some way? I am using VS2003. TIA Charles (Apologies for any bad VB in advance - I'm a C# person)
For each field add a corresponding Boolean field & property suffixed with Specified (and the property has attribute XmlIgnore), such as: Public Class Test Private m_Name As String = "George" Private m_NameSpecified As Boolean = False Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value m_NameSpecified = True End Set End Property <XmlIgnore()> Public Property NameSpecified() As Boolean Get Return m_NameSpecified End Get Set(ByVal Value As Boolean) m_NameSpecified = Value End Set End Property The framework on serializing each field looks for a field of type Boolean with the field name suffixed with "Specified". If this field exists and has value False, the field will not be serialized. Note that in the original field's Set accessor I set the XXXSpecified field to True. This means that if I ever set the property then it will get serialized; if I don't set it, it won't. If you specifically want it so that if it's not "George" it will be serialized then that is easy enough for you to change appropriately. Show quoteHide quote "Charles Law" <bl***@nowhere.com> wrote in message news:%232KE3mDJGHA.2668@tk2msftngp13.phx.gbl... >I have a complex object that I am serializing, and I would like to omit >elements that have a default value. > > For example, if I have a class as follows > > Public Class Test > > Private m_Name As String = "George" > Private m_Active As Boolean = False > Private m_Address As String > > Public Property Name() As String > Get > Return m_Name > End Get > Set(ByVal Value As String) > m_Name = Value > End Set > End Property > > Public Property Active() As Boolean > Get > Return m_Active > End Get > Set(ByVal Value As Boolean) > m_Active = Value > End Set > End Property > > Public Property Address() As String > Get > Return m_Address > End Get > Set(ByVal Value As String) > m_Address = Value > End Set > End Property > > End Class > > If Name is "George" when I serialize the object then I don't want this > element to be included in the output. Similarly, if Active is "False" when > the object is serialized, then I don't want that element included in the > output either. Anything different, and they should be included. > > Is there a way to do this, perhaps by tagging the property/element in some > way? I am using VS2003. > > TIA > > Charles > > 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 Show quoteHide quote "Clive Dixon" <clived.noluncheonm***@digita.noluncheonmeat.com> wrote in message news:eif4czaJGHA.3176@TK2MSFTNGP12.phx.gbl... > (Apologies for any bad VB in advance - I'm a C# person) > For each field add a corresponding Boolean field & property suffixed with > Specified (and the property has attribute XmlIgnore), such as: > > Public Class Test > > Private m_Name As String = "George" > Private m_NameSpecified As Boolean = False > > Public Property Name() As String > Get > Return m_Name > End Get > Set(ByVal Value As String) > m_Name = Value > m_NameSpecified = True > End Set > End Property > > <XmlIgnore()> > Public Property NameSpecified() As Boolean > Get > Return m_NameSpecified > End Get > Set(ByVal Value As Boolean) > m_NameSpecified = Value > End Set > End Property > > The framework on serializing each field looks for a field of type Boolean > with the field name suffixed with "Specified". If this field exists and > has value False, the field will not be serialized. > > Note that in the original field's Set accessor I set the XXXSpecified > field to True. This means that if I ever set the property then it will get > serialized; if I don't set it, it won't. If you specifically want it so > that if it's not "George" it will be serialized then that is easy enough > for you to change appropriately. > > "Charles Law" <bl***@nowhere.com> wrote in message > news:%232KE3mDJGHA.2668@tk2msftngp13.phx.gbl... >>I have a complex object that I am serializing, and I would like to omit >>elements that have a default value. >> >> For example, if I have a class as follows >> >> Public Class Test >> >> Private m_Name As String = "George" >> Private m_Active As Boolean = False >> Private m_Address As String >> >> Public Property Name() As String >> Get >> Return m_Name >> End Get >> Set(ByVal Value As String) >> m_Name = Value >> End Set >> End Property >> >> Public Property Active() As Boolean >> Get >> Return m_Active >> End Get >> Set(ByVal Value As Boolean) >> m_Active = Value >> End Set >> End Property >> >> Public Property Address() As String >> Get >> Return m_Address >> End Get >> Set(ByVal Value As String) >> m_Address = Value >> End Set >> End Property >> >> End Class >> >> If Name is "George" when I serialize the object then I don't want this >> element to be included in the output. Similarly, if Active is "False" >> when the object is serialized, then I don't want that element included in >> the output either. Anything different, and they should be included. >> >> Is there a way to do this, perhaps by tagging the property/element in >> some way? I am using VS2003. >> >> TIA >> >> Charles >> >> > >
Problème data tramsmission from tableadapter to ACCESS database
Odd or Even Whats's the problem using a enum like this? Is It Possible to Retrieve a List of Declared Variables? How to: Retrieving BuiltinDocumentProperties from Word Document late binding problem with option strict What happen to Find in Files in VS2005? Hpow to create a background process/application? convert rtf to html Can't get a value from my app.config file |
|||||||||||||||||||||||