C# 将类中的所有属性序列化为属性而不是元素

C# 将类中的所有属性序列化为属性而不是元素,c#,serialization,xml-serialization,C#,Serialization,Xml Serialization,我正在使用XmlSerializer将一些类序列化为XML文件。假设我有这个班: public class ClassToSerialize { public string PropertyA { get; set; } public string PropertyB { get; set; } public string PropertyC { get; set; } } 如果按原样序列化该类,我将得到: <ClassToSerialize> &l

我正在使用XmlSerializer将一些类序列化为XML文件。假设我有这个班:

public class ClassToSerialize
{
    public string PropertyA { get; set; }
    public string PropertyB { get; set; }
    public string PropertyC { get; set; }
}
如果按原样序列化该类,我将得到:

<ClassToSerialize>
    <PropertyA>{Value}</PropertyA}
    <PropertyB>{Value}</PropertyB}
    <PropertyC>{Value}</PropertyC}
</ClassToSerialize>
但是我有很多类,有很多属性。是否有任何方法可以在类级别或事件级别在XmlSerializer类中设置此选项


基于@ulugbek umirov给出的响应,我创建了以下代码,以将XmlAttribute属性应用于我的类和基类中的所有属性,以防其他人需要它。这段代码特定于我的类,因为它只适用于以“x”开头的类,但是如果您需要适应您的情况,这将很容易

private static void GenerateXmlAttributeOverrides(XmlAttributeOverrides overrides, Type type)
{
    foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
        {
            if ((propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType == typeof(string)))
            {
                if (!(propertyInfo.Name.EndsWith("Specified")
                    || HasAttribute(propertyInfo, typeof(XmlElementAttribute))
                    || HasAttribute(propertyInfo, typeof(XmlAttributeAttribute))))
                {
                    overrides.Add(type, propertyInfo.Name, new XmlAttributes { XmlAttribute = new XmlAttributeAttribute() });
                }
            }
            else if (propertyInfo.PropertyType.IsGenericType)
            {
                Type[] tipos = propertyInfo.PropertyType.GetGenericArguments();
                if (tipos != null && tipos.Length > 0 && tipos[0].Name.StartsWith("x"))
                    GenerateXmlAttributeOverrides(overrides, tipos[0]);
            }
            else if (propertyInfo.PropertyType.Name.StartsWith("x")) 
            {
                GenerateXmlAttributeOverrides(overrides, propertyInfo.PropertyType);
            }                
        }

    }
您可以将类的实例与XmlSerializer一起使用

你需要反思一下。以下是如何做到这一点的基本思路

static XmlAttributeOverrides GenerateXmlAttributeOverrides(Type type)
{
    XmlAttributeOverrides xmlAttributeOverrides = new XmlAttributeOverrides();
    foreach(PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
        xmlAttributeOverrides.Add(type, propertyInfo.Name, new XmlAttributes { XmlAttribute = new XmlAttributeAttribute() });
    return xmlAttributeOverrides;
}
用法:

XmlAttributeOverrides overrides = GenerateXmlAttributeOverrides(typeof(ClassToSerialize));
XmlSerializer serializer = new XmlSerializer(typeof(ClassToSerialize), overrides);
您可能希望添加检查属性是否为简单类型,并且该属性是否未缩写为
xmlement
xmldattribute
属性

static XmlAttributeOverrides GenerateXmlAttributeOverrides(Type type)
{
    XmlAttributeOverrides xmlAttributeOverrides = new XmlAttributeOverrides();
    foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
    {
        if ((propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType == typeof(string)) &&
            !propertyInfo.GetCustomAttributes().Any(a => a.GetType() == typeof(XmlElementAttribute) ||
                                                         a.GetType() == typeof(XmlAttributeAttribute)))
            xmlAttributeOverrides.Add(type, propertyInfo.Name, new XmlAttributes { XmlAttribute = new XmlAttributeAttribute() });
    }
    return xmlAttributeOverrides;
}

看起来很有趣。我会尽快尝试一下。谢谢,这真的很有帮助,我只想更正propertyInfo.GetType()返回的是propertyInfo对象的类型,而不是它所表示的属性的类型。为此,我使用了propertyInfo.PropertyType。
static XmlAttributeOverrides GenerateXmlAttributeOverrides(Type type)
{
    XmlAttributeOverrides xmlAttributeOverrides = new XmlAttributeOverrides();
    foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
    {
        if ((propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType == typeof(string)) &&
            !propertyInfo.GetCustomAttributes().Any(a => a.GetType() == typeof(XmlElementAttribute) ||
                                                         a.GetType() == typeof(XmlAttributeAttribute)))
            xmlAttributeOverrides.Add(type, propertyInfo.Name, new XmlAttributes { XmlAttribute = new XmlAttributeAttribute() });
    }
    return xmlAttributeOverrides;
}