繁体   English   中英

C#XmlIgnoreAttribute相似的属性名称

[英]C# XmlIgnoreAttribute similar property name

XmlIgnoreAttribute忽略属性的IsEnabledisEnabledFieldSpecified因为它们具有相似的名称。 如何解决这个问题? 结果,在我的属性secondCar我具有IsEnabled=false但是我希望得到IsEnabled=true 也许这是重复的问题,但我找不到堆栈上的答案。

class Program
{
    static void Main(string[] args)
    {
        Car firstCar = new Car() { IsEnabled = true };
        Car secondCar = new Car() { IsEnabled = false };

        secondCar = XmlUtility.XmlStr2Obj<Car>(XmlUtility.Obj2XmlStr(firstCar));

    }
}

[Serializable]
public class Car
{
    private bool isEnabledFieldSpecified;

    private bool isEnabledField;

    [XmlAttributeAttribute()]
    public bool IsEnabled
    {
        get
        {
            return this.isEnabledField;
        }
        set
        {
            this.isEnabledField = value;
        }
    }

    [XmlIgnoreAttribute()]
    public bool IsEnabledSpecified
    {
        get
        {
            return this.isEnabledFieldSpecified;
        }
        set
        {
            this.isEnabledFieldSpecified = value;
        }
    }
}

namespace Utils
{
    public class XmlUtility
    {
        public static string Obj2XmlStr(object obj, string nameSpace)
        {
            if (obj == null) return string.Empty;
            XmlSerializer sr = SerializerCache.GetSerializer(obj.GetType());

            StringBuilder sb = new StringBuilder();
            StringWriter w = new StringWriter(sb, CultureInfo.InvariantCulture);

            sr.Serialize(
                w,
                obj,
                new XmlSerializerNamespaces(
                new[]
                    {
                    new XmlQualifiedName("", nameSpace)
                    }
                ));
            return sb.ToString();
        }

        public static string Obj2XmlStr(object obj)
        {
            if (obj == null) return string.Empty;
            XmlSerializer sr = SerializerCache.GetSerializer(obj.GetType());

            StringBuilder sb = new StringBuilder();
            StringWriter w = new StringWriter(sb, CultureInfo.InvariantCulture);

            sr.Serialize(
                w,
                obj,
                new XmlSerializerNamespaces(new[] { new XmlQualifiedName(string.Empty) }));

            return sb.ToString();
        }

        public static T XmlStr2Obj<T>(string xml)
        {
            if (xml == null) return default(T);
            if (xml == string.Empty) return (T)Activator.CreateInstance(typeof(T));

            StringReader reader = new StringReader(xml);
            XmlSerializer sr = SerializerCache.GetSerializer(typeof(T));
            return (T)sr.Deserialize(reader);
        }

        public static XmlElement XmlStr2XmlDom(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return doc.DocumentElement;
        }

        public static XmlElement Obj2XmlDom(object obj, string nameSpace)
        {
            return XmlStr2XmlDom(Obj2XmlStr(obj, nameSpace));
        }
    }

    internal class SerializerCache
    {
        private static readonly Hashtable Hash = new Hashtable();
        public static XmlSerializer GetSerializer(Type type)
        {
            XmlSerializer res;
            lock (Hash)
            {
                res = Hash[type.FullName] as XmlSerializer;
                if (res == null)
                {
                    res = new XmlSerializer(type);
                    Hash[type.FullName] = res;
                }
            }
            return res;
        }
    }
}

尝试将两个对象的IsEnabledSpecified属性设置为true。 像这样:

Car firstCar = new Car() { IsEnabled = true, IsEnabledSpecified  = true };
Car secondCar = new Car() { IsEnabled = false, IsEnabledSpecified  = true };

这样,序列化程序将知道isEnabled属性应该被序列化。 IsEnabledSpecified属性对序列化程序具有特殊含义:它本身不会被序列化(由于XmlIgnore属性),但是它控制与它相关的xml元素是否与xml有效负载中的出现(“ Specified”属性设置为true)有关(指定”属性设置为false)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM