Partager via


XmlAttributes.XmlIgnore Propriété

Définition

Obtient ou définit une valeur qui spécifie si le XmlSerializer champ public ou la propriété en lecture/écriture publique est sérialisé ou non.

public:
 property bool XmlIgnore { bool get(); void set(bool value); };
public bool XmlIgnore { get; set; }
member this.XmlIgnore : bool with get, set
Public Property XmlIgnore As Boolean

Valeur de propriété

true si la XmlSerializer propriété ne doit pas sérialiser le champ ou la propriété ; sinon, false.

Exemples

L’exemple suivant sérialise une classe nommée Group, qui contient un membre nommé Comment auquel l’application XmlIgnoreAttribute est appliquée. L’exemple crée un XmlAttributes objet et définit la XmlIgnore propriété falsesur , remplaçant ainsi le XmlIgnoreAttribute.

using System;
using System.IO;
using 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 string GroupName;

   /* This field will be ignored when serialized--
      unless it's overridden. */
   [XmlIgnoreAttribute]
   public string Comment;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("IgnoreXml.xml");
   }

   // Return an XmlSerializer used for overriding.
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      /* Setting XmlIgnore to false overrides the XmlIgnoreAttribute
         applied to the Comment field. Thus it will be serialized.*/
      attrs.XmlIgnore = false;
      xOver.Add(typeof(Group), "Comment", attrs);

      /* Use the XmlIgnore to instruct the XmlSerializer to ignore
         the GroupName instead. */
      attrs = new XmlAttributes();
      attrs.XmlIgnore = true;
      xOver.Add(typeof(Group), "GroupName", attrs);

      XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
      return xSer;
   }

   public void SerializeObject(string filename)
   {
      // Create an XmlSerializer instance.
      XmlSerializer xSer = CreateOverrider();

      // Create the object to serialize and set its properties.
      Group myGroup = new Group();
      myGroup.GroupName = ".NET";
      myGroup.Comment = "My Comment...";

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Serialize the object and close the TextWriter.
      xSer.Serialize(writer, myGroup);
      writer.Close();
   }
}
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()
        t.SerializeObject("IgnoreXml.xml")
    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)
        writer.Close()
    End Sub
End Class

Remarques

Par défaut, tous les champs publics et les propriétés de lecture/écriture publiques sont sérialisés par le XmlSerializer. Autrement dit, la valeur de chaque champ ou propriété public est conservée en tant qu’élément XML ou attribut XML dans une instance de document XML.

Pour remplacer la sérialisation par défaut d’un champ ou d’une propriété, créez un XmlAttributes objet et définissez sa XmlIgnore propriété truesur . Add objet à un XmlAttributeOverrides objet et spécifiez le type de l’objet qui contient le champ ou la propriété à ignorer, ainsi que le nom du champ ou de la propriété à ignorer.

Si un XmlIgnoreAttribute champ ou une propriété est appliqué, le champ ou la propriété est ignoré. Toutefois, vous pouvez remplacer ce comportement en créant un XmlAttributes objet, en définissant sa XmlIgnore propriété falsesur , en l’ajoutant à un XmlAttributeOverrides objet spécifiant le type de l’objet qui contient le champ ou la propriété, et le nom du champ ou de la propriété.

S’applique à

Voir aussi