2012-03-26 2 views
0
내가 개체를 XML로 직렬화하기 위해 시도하는 다음과 같은 기능을 사용하고

..개체를 XML로 직렬화 : IList의는 <CustomObject은> 속성 원인 예외

public static string SerializeObject<T>(T obj) 
     { 
      try 
      { 
       string xmlString = null; 
       MemoryStream memoryStream = new MemoryStream(); 
       XmlSerializer xs = new XmlSerializer(typeof(T)); 
       XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
       xs.Serialize(xmlTextWriter, obj); 
       memoryStream = (MemoryStream)xmlTextWriter.BaseStream; 
       xmlString = UTF8ByteArrayToString(memoryStream.ToArray()); return xmlString; 
      } 
      catch (Exception ex) 
      { 
       return string.Empty; 
      } 
     } 

IList의 속성을 가진 개체를 직렬화하려고 시도 그 안에는 다음과 같은 예외가 있습니다.

Cannot serialize member 'ObjectModel.Order.LineItems' of type 'System.Collections.Generic.IList 

누군가이 시나리오에 맞게 기능을 변경하도록 도와 줄 수 있습니까?

입력 코드를 살펴보기 위해 기존 코드를 사용할 수 있습니까? 유형이 Ilist 인 경우 목록으로 변경됩니까? somoeone 가능하다면 코드를 도와 주실 수 있습니까 ??

답변

6

이에 대한 훌륭한 솔루션이 경우 List<T> 등의 구체적인 유형을 사용하여 만 해결 방법은 없습니다 - 당신은 기존 속성을 List<T>으로 변경하거나 List<T> 유형의 serialization에 사용 된 추가 속성을 추가 할 수 있습니다 (XML은 기존 속성 무시).

+0

나는 입력 개체에서 IList의 확인 및 IT 변화를 존재하는 경우는 즉시 ToList? –

+0

그게 가능하지 않다면 클래스 정의를 변경해야합니다. – BrokenGlass