0

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.

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.

Here's how the class has been defined:

 [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; }

    }

Now, if I do

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

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

[XmlElement("DocumentCode")]

and sees it's a duplicate tag.

Anyway I have an

[IWantToSerializeThisAttribute]

that makes the 2 properties different.

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

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

Thanks everyone

2 Answers2

0

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

This worked for me:

 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);
            };

Cheers

Liquid Core
  • 1
  • 6
  • 27
  • 52
  • This is what I was looking for, because allows me to do dynamic xml serialization/deserialization excluding fields only when I need it –  Apr 27 '17 at 08:07
0

Not sure if I understand correctly but let me try to give you some options:

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

If you want to serialize only specific properties use [XmlIgnore] attribute for the properties you want to omit. But if for some reason (DOC_CODE_FOR_EMPLOYEES and DOC_ID_FOR_EMPLOYEES)

But if you mean that it should be only omitted while deserializing, but serialization should still be done for all properties I'd think about implementing IXmlSerializable. This way you can specifically provide how would you like to read/write your xml.

Michal Delura
  • 71
  • 1
  • 6