나는이 오류를 던지고 다음 코드 ...XmlAttributeOverrides
오류 ... 배열이 아닌 유형의
, 다음과 같은 속성을 사용할 수 있습니다 : XmlAttribute, XmlText, XmlElement 또는 XmlAnyElement.
코드 (이동 방법에서 마지막 줄은 예외를 던지고있다) ...
public void Go(Type typeToSerialize, object itemToSerialize)
{
Dictionary<string, bool> processedList = new Dictionary<string, bool>();
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
AttachXmlTransforms(overrides, itemToSerialize.GetType(), processedList);
s = new XmlSerializer(typeToSerialize, overrides);
}
private static void AttachXmlTransforms(XmlAttributeOverrides overrides, Type root,
Dictionary<string, bool> processedList)
{
foreach (PropertyInfo pi in root.GetProperties())
{
string keyName = pi.DeclaringType + "-" + pi.Name;
if ((pi.PropertyType == typeof(DateTime) || pi.PropertyType == typeof(DateTime?))
&& !processedList.ContainsKey(keyName))
{
XmlAttributes attributes = new XmlAttributes();
attributes.XmlElements.Add(new XmlElementAttribute(pi.Name));
//attributes.XmlAnyAttribute = new XmlAnyAttributeAttribute();
attributes.XmlAttribute = new XmlAttributeAttribute("dval");
//attributes.XmlIgnore = true;
processedList.Add(keyName, true);
overrides.Add(pi.DeclaringType, pi.Name, attributes);
}
if (pi.MemberType == MemberTypes.Property && !pi.PropertyType.IsPrimitive
&& pi.PropertyType.IsPublic && pi.PropertyType.IsClass
&& pi.PropertyType != typeof(DateTime))
{
AttachXmlTransforms(overrides, pi.PropertyType, processedList);
}
}
}
나는이 외부 요구 사항입니다 (단 날짜 시간 요소 (dval) 속성을 추가하려고 해요)이에서
... 이것에<CreatedDate>01/01/2012</CreatedDate>
...
<CreatedDate dval="01/01/2012">01/01/2012</CreatedDate>
일반 비 배열 유형 요소에 속성을 추가하는 방법이 있습니까?
주 객체를 변환 : 아주 impor의입니다 이와 같이 작성된 시리얼 라이저를 재사용하지 않거나 메모리 누수가 발생합니다. –