Question

When I add an "XmlAttribute/XmlElement" to a property in an Interface and 100 other classes inherit from this interface, when Serializing the object to XML - why doesn't the attributename I entered show up in the XML file after serialization?

interface Test
{
    [XmlAttribute("Name")]
    bool PropertyName { get; set; }
}

when saving the file, is shows "PropertyName" and not "Name".

Is there any way to make it work, so a proeprty with an XmlAttributes added to an interface changes the Value everywhere instead of Value because if a few classes inherit from the same interface it takes alot of time to add all those Attributes to all those classes that inherit it, and it would be easier to change the Name of the Attribute instead of changing a 100 of them.

Was it helpful?

Solution

Deukalin, spent some time and make it work with comprehensive tree of classes and interfaces.

Here is the working code

    public interface ITestX
    {
        [XmlAttribute("NameX")]
        string PropertyNameX { get; set; }
    }

    public interface ITestY
    {
        [XmlAttribute("NameY")]
        string PropertyNameY { get; set; }
    }

    public interface ITestZ
    {
        [XmlAttribute("NameZ")]
        string PropertyNameZ { get; set; }
    }

    public abstract class TestC : ITestZ
    {
        public abstract string PropertyNameZ { get; set; }
    }

    public abstract class TestA : TestC, ITestX, ITestY
    {
        public abstract string PropertyNameX { get; set; }
        public abstract string PropertyNameY { get; set; }
    }

    public class TestB : TestA
    {
        public override string PropertyNameX { get; set; }
        public override string PropertyNameY  { get; set; }
        public override string PropertyNameZ { get; set; }
    }

    public static class ClassHandler
    {
        public static T GetCustomAttribute<T>(this PropertyInfo propertyInfo, bool inherit) where T : Attribute
        {
            object[] attributes = propertyInfo.GetCustomAttributes(typeof(T), inherit);

            return attributes == null || attributes.Length == 0 ? null : attributes[0] as T;
        }

        public static void GetXmlAttributeOverrides(XmlAttributeOverrides overrides, Type type)
        {
            if (type.BaseType != null)
            {
                GetXmlAttributeOverrides(overrides, type.BaseType);
            }

            foreach (Type derived in type.GetInterfaces())
            {
                foreach (PropertyInfo propertyInfo in derived.GetProperties())
                {
                    XmlAttributeAttribute xmlAttributeAttribute = ClassHandler.GetCustomAttribute<XmlAttributeAttribute>(propertyInfo, true) as XmlAttributeAttribute;

                    if (xmlAttributeAttribute == null)
                        continue;

                    XmlAttributes attr1 = new XmlAttributes();
                    attr1.XmlAttribute = new XmlAttributeAttribute();
                    attr1.XmlAttribute.AttributeName = xmlAttributeAttribute.AttributeName;
                    overrides.Add(type, propertyInfo.Name, attr1);
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            XmlAttributeOverrides XmlAttributeOverrides = new XmlAttributeOverrides();
            ClassHandler.GetXmlAttributeOverrides(XmlAttributeOverrides, typeof(TestB));
            try
            {
                TestB xtest = new TestB() { PropertyNameX = "RajX", PropertyNameY = "RajY", PropertyNameZ = "RajZ" };

                StringBuilder xmlString = new StringBuilder();
                using (XmlWriter xtw = XmlTextWriter.Create(xmlString))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(TestB), XmlAttributeOverrides);
                    serializer.Serialize(xtw, xtest);

                    xtw.Flush();
                }

                Console.WriteLine(xmlString.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
}

Below is the output of sample above

<?xml version="1.0" encoding="utf-16"?><TestB xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" NameZ="RajZ" Na
meX="RajX" NameY="RajY" />
Press any key to continue . . .

OTHER TIPS

I altered the code to suit my project more. What I did was this:

    public static XmlAttributeOverrides GetXmlAttributeOverrides(Type type)
    {
        XmlAttributeOverrides overrides = new XmlAttributeOverrides();

        foreach (Type derived in ClassHandler.GetImplementedInterfaces(type))
        {

            foreach (PropertyInfo propertyInfo in derived.GetProperties())
            {
                XmlAttributeAttribute xmlAttributeAttribute = ClassHandler.GetCustomAttribute<XmlAttributeAttribute>(propertyInfo, true) as XmlAttributeAttribute;

                if (xmlAttributeAttribute == null) continue;

                XmlAttributes attr1 = new XmlAttributes();
                attr1.XmlAttribute = new XmlAttributeAttribute();
                attr1.XmlAttribute.AttributeName = xmlAttributeAttribute.AttributeName;
                overrides.Add(type, propertyInfo.Name, attr1);
            }
        }

        return overrides;
    }

The object I'm trying on which implements an inteface with properties all have "[XmlAttributeAttribute(SomeName)] over them.

Still, when I serialize it gives the same results. I don't get the Attribute value from the interface.

This is how I serialize:

    public static void SerializeFile(String filename, object obj, bool deleteIfExists = true)
    {

        if (deleteIfExists)
        {
            FileManager.DeleteFile(filename);
        }

        Type[] extraTypes = ClassHandler.GetPropertiesTypes(obj, true);

        using (var stream = new FileStream(filename, FileMode.Create))
        {
            //XmlSerializer xmlSerialize = new XmlSerializer(obj.GetType(), extraTypes); 
            XmlSerializer xmlSerialize = new XmlSerializer(obj.GetType(), GetXmlAttributeOverrides(obj.GetType()), extraTypes, null, null);
            xmlSerialize.Serialize(stream, obj);
        }
    }

the two methods I use from my ClassHandler class:

    public static T GetCustomAttribute<T>(this PropertyInfo propertyInfo, bool inherit) where T : Attribute
    {
        object[] attributes = propertyInfo.GetCustomAttributes(typeof(T), inherit);

        return attributes == null || attributes.Length == 0 ? null : attributes[0] as T;
    }

    public static List<Type> GetImplementedInterfaces(Type type)
    {
        Type[] types = type.GetInterfaces();
        List<Type> lTypes = new List<Type>();

        foreach(Type t in types)
        {
            lTypes.Add(t);
        }

        return lTypes;
    }

classes have the following structure:

interface IAnimal
{
    // Properties
    // Methods
}

public abstract class Animal : IAnimal
{
    // Implements IAnimal properties and methods
    // This XmlElement gets written correctly when XML Serializing
    // Example:
    [XmlElement("AnimalAge")]
    public double Age
    {
        get { return _age; }
        set { _age = value; }
    }
}

public abstract class Bird : Animal, IAttributeWings
{
    // Implements Attributes common for all "Birds"
    // Setting "override" here gives me error
    public bool HasWings { get { return _hasWings; } set { _hasWings = value; } }
}

public class Pelican : Bird, IAttributeCanFly
{
    // Implements Attributes common for all "Pelicans"
    // Does not implement previous attribute IAttributeWings since Bird class does this
    // Setting "override" here gives me error as well
    public bool CanFly { get { return _canFly; } set { _canFly = value; } }
}

and then the attribute interfaces only have properties like "bool CanFly, bool hasWings" and such and other attributes for a specific category like in this example.

Some reason it does not work in .NET... But I come up with this solution to overcome the problem you have using XmlAttributeOverrides. For more information about it, please visit the below link

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeoverrides.aspx

You can optimize it by refactoring / caching this GetXmlAttributeOverrides() method somewhere in your application. Hope it helps.

public interface ITest
{
    [XmlAttribute("Name")]
    string PropertyName { get; set; }
}

public class XTest : ITest
{
    public string PropertyName
    {
        get;
        set;
    }
}

public class Program
{
   static void Main(string[] args)
    {

        try
        {
            XTest xtest = new XTest() { PropertyName = "Raj" };

            StringBuilder xmlString = new StringBuilder();
            using (XmlWriter xtw = XmlTextWriter.Create(xmlString))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(XTest), GetXmlAttributeOverrides(typeof(XTest)));
                serializer.Serialize(xtw, xtest);

                xtw.Flush();
            }

            Console.WriteLine( xmlString.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

   public static XmlAttributeOverrides GetXmlAttributeOverrides(Type derivedType)
   {
       Type type = typeof(ITest);
       XmlAttributeOverrides overrides = new XmlAttributeOverrides();

       XmlAttributes attr = new XmlAttributes();

       foreach (PropertyInfo propertyInfo in type.GetProperties())
       {
           XmlAttributeAttribute xmlAttributeAttribute = propertyInfo.GetCustomAttribute(typeof(XmlAttributeAttribute), true) as XmlAttributeAttribute;

           if (xmlAttributeAttribute == null) continue;

           XmlAttributes attr1 = new XmlAttributes();
           attr1.XmlAttribute = new XmlAttributeAttribute();
           attr1.XmlAttribute.AttributeName = xmlAttributeAttribute.AttributeName;
           overrides.Add(derivedType, propertyInfo.Name, attr1);
       }

       return overrides;
   }
}

EDIT: Include this extension class in your application

public static class PropertyInfoEx
{
        public static T GetCustomAttribute<T>(this PropertyInfo propertyInfo, bool inherit) where T : Attribute
        {
            object[] attributes = propertyInfo.GetCustomAttributes(typeof(T), inherit);

            return attributes == null || attributes.Length == 0 ? null : attributes[0] as T;
        }

}

Found some issues in your code. Below is the corrected code

public static XmlAttributeOverrides GetXmlAttributeOverrides(Type type)
{
    XmlAttributeOverrides overrides = new XmlAttributeOverrides();

    // Get all interfaces that the "type" implements (is it same as "derivedType" from previously)?
    foreach (Type derived in ClassHandler.GetImplementedInterfaces(type))
    {

        foreach (PropertyInfo propertyInfo in derived.GetProperties())
        {
            XmlAttributeAttribute xmlAttributeAttribute = ClassHandler.GetCustomAttribute<XmlAttributeAttribute>(propertyInfo, true) as XmlAttributeAttribute;

            if (xmlAttributeAttribute == null) continue;

            XmlAttributes attr1 = new XmlAttributes();
            attr1.XmlAttribute = new XmlAttributeAttribute();
            attr1.XmlAttribute.AttributeName = xmlAttributeAttribute.AttributeName;
            overrides.Add(type, propertyInfo.Name, attr1);
        }
    }

    return overrides;
}

Try for yourself. Let me know if it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top