![](/img/trans.png)
[英]Custom Json Serializer to serialize and deserialize all properties by ignoring the class attributes
[英]Serialize all properties in a class as attributes instead of elements
我正在使用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>
我希望將屬性序列化為xml屬性,而不是元素。 我知道我可以通過在每個屬性中使用[XmlAttribute]實現此目的:
public class ClassToSerialize
{
[XmlAttribute]
public string PropertyA { get; set; }
[XmlAttribute]
public string PropertyB { get; set; }
[XmlAttribute]
public string PropertyC { get; set; }
}
但是我有很多類,有很多特性。 有什么辦法可以在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);
}
}
}
您可以將XmlAttributeOverrides類的實例與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);
您可能希望添加檢查屬性是否為簡單類型,以及是否不使用XmlElement
或XmlAttribute
屬性縮寫。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.