Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

XmlAttributes.XmlIgnore Property

Gets or sets a value that specifies whether or not the XmlSerializer serializes a public field or public read/write property.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)
Public Property XmlIgnore As Boolean

Property Value

Type: System.Boolean
true if the XmlSerializer must not serialize the field or property; otherwise, false.

By default, all public fields and public read/write properties are serialized by the XmlSerializer. That is, the value of each public field or property is persisted as an XML element or XML attribute in an XML-document instance.

To override the default serialization of a field or property, create an XmlAttributes object, and set its XmlIgnore property to true. Add the object to an XmlAttributeOverrides object and specify the type of the object that contains the field or property to ignore, and the name of the field or property to ignore.

If an XmlIgnoreAttribute is applied to a field or property, the field or property is ignored. However you can override that behavior by creating an XmlAttributes object, setting its XmlIgnore property to false, adding it to an XmlAttributeOverrides object specifying the type of the object that contains the field or property, and the name of the field or property.

The following example serializes a class named Group, which contains a member named Comment to which the XmlIgnoreAttribute is applied. The example creates an XmlAttributes object, and sets the XmlIgnore property to false, thereby overriding the XmlIgnoreAttribute.

Imports System
Imports System.IO
Imports System.Xml.Serialization

' This is the class that will be serialized.  
Public Class Group
    ' The GroupName value will be serialized--unless it's overridden. 
    Public GroupName As String 

    ' This field will be ignored when serialized-- 
    '  unless it's overridden.
    <XmlIgnoreAttribute()> Public Comment As String 
End Class 

Public Class Test

    Public Shared Sub Main()
        Dim t As New Test()
    End Sub 

    ' Return an XmlSerializer used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributeOverrides and XmlAttributes objects. 
        Dim xOver As New XmlAttributeOverrides()
        Dim attrs As New XmlAttributes()

        ' Setting XmlIgnore to false overrides the XmlIgnoreAttribute 
        ' applied to the Comment field. Thus it will be serialized.
        attrs.XmlIgnore = False
        xOver.Add(GetType(Group), "Comment", attrs)

        ' Use the XmlIgnore to instruct the XmlSerializer to ignore 
        ' the GroupName instead. 
        attrs = New XmlAttributes()
        attrs.XmlIgnore = True
        xOver.Add(GetType(Group), "GroupName", attrs)

        Dim xSer As New XmlSerializer(GetType(Group), xOver)
        Return xSer
    End Function 

    Public Sub SerializeObject(ByVal filename As String)
        ' Create an XmlSerializer instance. 
        Dim xSer As XmlSerializer = CreateOverrider()

        ' Create the object to serialize and set its properties. 
        Dim myGroup As New Group()
        myGroup.GroupName = ".NET"
        myGroup.Comment = "My Comment..." 

        ' Writing the file requires a TextWriter. 
        Dim writer As New StreamWriter(filename)

        ' Serialize the object and close the TextWriter.
        xSer.Serialize(writer, myGroup)
    End Sub 
End Class

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.