2014-04-14 2 views
0

DatetimeFormatInfo 형식의 개체를 serialize하려면. DateTimeFormatInfo 유형의 객체를 직렬화하는 방법은 무엇입니까?

나는 다음과 같은 코드를 시도 :

DateTimeFormatInfo dateTimeFormat = new DateTimeFormatInfo(); 
dateTimeFormat.ShortDatePattern = "dd-MMM-yy"; 
xs = new XmlSerializer(dateTimeFormat.GetType()); 
StreamWriter sw = new StreamWriter("Setting.xml"); 
xs.Serialize(sw, dateTimeFormat); 

을하지만 아래의 예외가 발생합니다.

System.InvalidOperationException이 처리되지 않았습니다.
XML 문서를 생성하는 동안 오류가 발생했습니다.
종류 System.Globalization.GregorianCalendar 아니 었습니다.
XmlInclude 또는 SoapInclude 특성을 정적으로 알 수없는 형식을 지정하려면 XmlInclude를 사용하십시오.

DateTimeFormatInfo를 serialize하기 위해 추가해야하는 항목이 있습니까?

+1

직렬 변환기는 직렬화 할 수없는 개체를 포함 할 수있는 개체 그래프를 탐색합니다. 대신에 날짜 패턴을 직렬화하는 것이 좋습니다. – Romoku

답변

0

XmlSerializer에 직렬화 할 중독 개체 유형 목록을 포함해야합니다. 귀하의 경우에는 객체 유형 System.Globalization.GregorianCalendar을 추가해야합니다.

 System.Globalization.DateTimeFormatInfo dateTimeFormat = new System.Globalization.DateTimeFormatInfo(); 
     dateTimeFormat.ShortDatePattern = "dd-MMM-yy"; 

     // Add here all the extra types you need to serialize 
     Type[] extraTypes = new Type[] { typeof(System.Globalization.GregorianCalendar) }; 

     System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(dateTimeFormat.GetType(), extraTypes); 
     System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\testso.xml"); 
     xs.Serialize(sw, dateTimeFormat); 
관련 문제