点击或拖拽改变大小

XmlTextWriterIgnoreAttributeType 属性

获取或设置指示Serializer 方法Divide(ITextWriter, Object, Object) 进行序列化的公共字段或公共读 / 写属性值。

命名空间:  XPatchLib
程序集:  XPatchLib (在 XPatchLib.dll 中) 版本:3.0.5.10
语法
C#
public Type IgnoreAttributeType { get; set; }

属性值

类型: Type
默认值: XmlIgnoreAttribute

实现

ITextWriterIgnoreAttributeType
备注
用于控制如何Serializer 方法 Divide(ITextWriter, Object, Object) 序列化对象。
示例

下面的示例展示了 IgnoreAttributeType 的使用效果,以及如何修改 IgnoreAttributeType 。

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace XPatchLib.Example
{
    public class IgnoreAttributeType
    {
        public static void Main()
        {
            var c1 = new XmlIgnoreClass { A = "A", B = "B" };
            var c2 = new XmlIgnoreClass { A = "C", B = "D" };

            //输出内容如下:
            /*
             * <?xml version=""1.0"" encoding=""utf-16""?>
             * <XmlIgnoreClass>
             *   <B>D</B>
             *   </XmlIgnoreClass>
             */
            Console.WriteLine(UseDefaultIgnoreAttribute(c1, c2));

            //输出内容如下:
            /*
             * <?xml version=""1.0"" encoding=""utf-16""?>
             * <XmlIgnoreClass>
             *   <A>C</A>
             *   <B>D</B>
             *   </XmlIgnoreClass>
             */
            Console.WriteLine(SetIgnoreAttributeToNull(c1, c2));

            //输出内容如下:
            /*
             * <?xml version=""1.0"" encoding=""utf-16""?>
             * <XmlIgnoreClass>
             *   <A>C</A>
             *   </XmlIgnoreClass>
             */
            Console.WriteLine(SetIgnoreAttributeToNull(c1, c2));
        }

        public static string UseDefaultIgnoreAttribute(XmlIgnoreClass c1, XmlIgnoreClass c2)
        {
            StringBuilder result = new StringBuilder();
            Serializer serializer = new Serializer(typeof(XmlIgnoreClass));
            using (StringWriter writer = new StringWriter(result))
            {
                using (XmlTextWriter xmlWriter = new XmlTextWriter(writer))
                {
                    //序列化前不变更默认的IgnoreAttributeType设置。
                    serializer.Divide(xmlWriter, c1, c2);
                }
            }
            return result.ToString();
        }

        public static string SetIgnoreAttributeToNull(XmlIgnoreClass c1,XmlIgnoreClass c2)
        {
            StringBuilder result = new StringBuilder();
            Serializer serializer = new Serializer(typeof(XmlIgnoreClass));
            using (StringWriter writer = new StringWriter(result))
            {
                using (XmlTextWriter xmlWriter = new XmlTextWriter(writer))
                {
                    //在本次序列化过程中,类型定义中的XmlIgnoreAttribute无效。
                    xmlWriter.IgnoreAttributeType = null;
                    serializer.Divide(xmlWriter, c1, c2);
                }
            }
            return result.ToString();
        }

        public static string SetIgnoreAttributeToOther(XmlIgnoreClass c1, XmlIgnoreClass c2)
        {
            StringBuilder result = new StringBuilder();
            Serializer serializer = new Serializer(typeof(XmlIgnoreClass));
            using (StringWriter writer = new StringWriter(result))
            {
                using (XmlTextWriter xmlWriter = new XmlTextWriter(writer))
                {
                    //在本次序列化过程中,使用 XPatchLibXmlIgnoreAttribute 作为忽略特性,而不是用默认的 XmlIgnoreAttribute
                    xmlWriter.IgnoreAttributeType = typeof(XPatchLibXmlIgnoreAttribute);
                    serializer.Divide(xmlWriter, c1, c2);
                }
            }
            return result.ToString();
        }
    }
    public class XmlIgnoreClass
    {
        [System.Xml.Serialization.XmlIgnore]
        public string A { get; set; }

        [XPatchLibXmlIgnore]
        public string B { get; set; }
    }

    [AttributeUsage(AttributeTargets.Property)]
    public class XPatchLibXmlIgnoreAttribute : Attribute
    {

    }
}
版本信息

.NET Framework

支持版本:4.0, 3.5, 2.0

.NET Standard

支持版本:1.3, 1.0
参见