ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemXmlSerializationXmlAttributesClassXmlIgnoreTopic.htmXmlAttributes.XmlIgnore 属性全部显示
获取或设置一个值,该值指定 XmlSerializer 是否序列化公共字段或公共读/写属性。[Visual Basic]
Public Property XmlIgnore As Boolean
[C#]
public bool XmlIgnore {get; set;}属性值
如果 XmlSerializer 不应序列化字段或属性,则为 true;否则为 false。
备注
默认情况下,所有公共字段和公共读/写属性由 XmlSerializer 序列化。即每个公共字段或属性的值作为 XML 元素或 XML 特性保持在 XML 文档实例中。
若要重写字段或属性的默认序列化,创建一个 XmlAttributes 对象,并将其 XmlIgnore 属性设置为 true。将该对象 Add 到 XmlAttributeOverrides 对象中,同时指定包含要忽略的字段或属性的对象的类型以及要忽略的字段或属性的名称。
如果将 XmlIgnoreAttribute 应用于一个字段或属性,则将忽略该字段或属性。但是,可以通过创建一个 XmlAttributes 对象、将其 XmlIgnore 属性设置为 false,并将它添加到 XmlAttributeOverrides 对象(该对象指定包含字段或属性的对象的类型以及字段或属性的名称)来重写该行为。
示例
[Visual Basic, C#, C++] 下面的示例序列化一个名为 Group 的类,该类包含一个名为 Comment 并且对其应用了 XmlIgnoreAttribute 的成员。该示例创建一个 XmlAttributes 对象,并将其 XmlIgnore 属性设置为 false,从而重写 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();
   }
}