本文介绍了C#中的XmlSerializer,通过特定属性反序列化由多个具有相同名称的XmlElement装饰的类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用错误的方式用XmlElement装饰的类,但是它也有一些属性,可以让我识别出我需要的字段.

I have a class which is decorated with XmlElement in a wrong way, but it has also attributes that could allow me to identify the fields I need.

我只能修改[IWantToSerializeThisAttribute]并将其他属性添加到MySerializableClass,因为对属性名称或XmlElement名称的任何修改都会涉及大量的代码维护工作.

I can only modify [IWantToSerializeThisAttribute] and add other attributes to MySerializableClass because any modification to property names or XmlElement names would involve heavy coding maintenance.

这是定义类的方式:

 [XmlRoot("ARandomXmlRoot")]
    public class MySerializableClass
    {

        //CAMPI DIR_DOCUMENTI
        //[MetadatoDocumentoAlfresco] è un attributo che serve per selezionare i campi per l'aggiornamento dati massivo su alfresco
        [IWantToSerializeThisAttribute]
        [XmlElement("DocumentCode")]
        public string DOC_CODE { get; set; }

        [IWantToSerializeThisAttribute]
        [XmlElement("DocumentId")]
        public string DOC_ID { get; set; }

        [XmlElement("DocumentCode")]
        public string DOC_CODE_FOR_EMPLOYEES { get; set; }

        [XmlElement("DocumentId")]
        public string DOC_ID_FOR_EMPLOYEES { get; set; }

    }

现在,如果我愿意

XmlSerializer.Deserialize(xmlString, typeof(MySerializableClass));

我最有可能收到错误消息,因为XmlSerializer的查找次数是该值的2倍

I will get an error most probably because XmlSerializer is finding 2 times the

[XmlElement("DocumentCode")]

发现它是重复的标签.

反正我有一个

[IWantToSerializeThisAttribute]

这两个属性不同.

我能否以某种方式告诉XmlSerializer.Deserialize仅捕获和定值"IwantToSerializeThisAttribute"属性,而忽略其他属性?

Can I tell someway XmlSerializer.Deserialize to catch and valorize only the "IwantToSerializeThisAttribute" properties and ignore the others?

我无法使用XmlOverrideAttributes更改序列化,但是也许在反序列化过程中有一些方法可以实现.

I cannot change serialization with XmlOverrideAttributes, but maybe there is some way to do it during deserialization.

谢谢大家

推荐答案

尝试使用XmlOverrideAttributes和Reflection.使用LINQ只是为了使其简短.

Try with XmlOverrideAttributes and Reflection. Using LINQ just to make it short.

这对我有用:

 string XmlString = "<ARandomXmlRoot> XML HERE </ARandomXmlRoot>";
            XmlAttributeOverrides overrides = new XmlAttributeOverrides();

            //Select fields I DON'T WANT TO SERIALIZE because they throw exception
            string[] properties = (new MySerializableClass())
                .GetType().GetProperties()
                .Where(p => !Attribute.IsDefined(p, typeof(IWantToSerializeThisAttribute)))
                .Select(p => p.Name);

            //Add an XmlIgnore attribute to them
            properties.ToList().ForEach(field => overrides.Add(typeof(MySerializableClass), field, new XmlAttributes() { XmlIgnore = true }));

            MySerializableClass doc = new MySerializableClass();

            XmlSerializer serializerObj = new XmlSerializer(typeof(MySerializableClass), overrides);
            using (StringReader reader = new StringReader(xmlString))
            {
                doc = (MySerializableClass)serializerObj.Deserialize(reader);
            };

欢呼

这篇关于C#中的XmlSerializer,通过特定属性反序列化由多个具有相同名称的XmlElement装饰的类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:50