接口中的XmlAttribute / XmlElement(序列化时元素名称未更改) - 为什么? [英] XmlAttribute / XmlElement in Interface (Element Name not changed when Serialized) - Why?

查看:187
本文介绍了接口中的XmlAttribute / XmlElement(序列化时元素名称未更改) - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向接口中的属性添加XmlAttribute / XmlElement并且从该接口继承100个其他类时,将对象序列化为XML时 - 为什么我输入的属性名在序列化后不显示在XML文件中?

  interface Test 
{
[XmlAttribute(Name)]
bool PropertyName {get;组;保存文件时,
}

显示PropertyName而不是Name 。



有没有办法让它工作,所以添加到接口的XmlAttributes的proeprty会改变Value到处而不是Value,因为如果有几个类继承了相同的界面需要很多时间将所有这些属性添加到继承它的所有类中,并且更改属性的名称而不是更改其中的100个将更容易。



这是工作代码

 公共接口ITestX 
{
[XmlAttribute(NameX)]
string PropertyNameX {get;组; }
}

公共接口ITestY
{
[XmlAttribute(NameY)]
string PropertyNameY {get;组; }
}

公共接口ITestZ
{
[XmlAttribute(NameZ)]
string PropertyNameZ {get;组; }
}

公共抽象类TestC:ITestZ
{
public abstract string PropertyNameZ {get;组; }
}

公共抽象类TestA:TestC,ITestX,ITestY
{
public abstract string PropertyNameX {get;组; }
public abstract string PropertyNameY {get;组; }
}

公共类TestB:TestA
{
公共覆盖字符串PropertyNameX {get;组; }
公共覆盖字符串PropertyNameY {get;组; }
公共覆盖字符串PropertyNameZ {get;组; }
}

公共静态类ClassHandler
{
public static T GetCustomAttribute< T>(此PropertyInfo propertyInfo,bool inherit)其中T:Attribute
{
object [] attributes = propertyInfo.GetCustomAttributes(typeof(T),inherit);

返回属性== null || attributes.Length == 0? null:attributes [0]为T;
}

public static void GetXmlAttributeOverrides(XmlAttributeOverrides覆盖,类型类型)
{
if(type.BaseType!= null)
{
GetXmlAttributeOverrides(覆盖,类型.BaseType);
}

foreach(类型派生类型.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);
}
}
}
}

类程序
{
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());
}
}

以下是上面样本的输出

 <?xml version =1.0encoding =utf-16?>< TestB xmlns:xsi =http:/ /www.w3.org/2001/ 
XMLSchema-instancexmlns:xsd =http://www.w3.org/2001/XMLSchemaNameZ =RajZNa
meX =RajX NameY =RajY/>
按任意键继续。 。 。


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.

解决方案

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 . . .

这篇关于接口中的XmlAttribute / XmlElement(序列化时元素名称未更改) - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆