2010-06-17 160 views
1

Camarades,XML序列化和反序列化問題XML結構

我遇到以下問題。捕獲一個列表Struct,Serialize(有效的W3C)併發送到一個WebService。在我接收的WebService中,轉換爲一個字符串,W3C和Deserializer都有效,但是當我嘗試運行它時,總會出現錯誤,說有些對象沒有關閉。

任何幫助?

發送代碼:

#region ListToXML 
    private XmlDocument ListToXMLDocument(object __Lista) 
    { 
     XmlDocument _ListToXMLDocument = new XmlDocument(); 

     try 
     { 
      XmlDocument _XMLDoc  = new XmlDocument(); 
      MemoryStream _StreamMem = new MemoryStream(); 
      XmlSerializer _XMLSerial = new XmlSerializer(__Lista.GetType()); 

      StreamWriter _StreamWriter = new StreamWriter(_StreamMem, Encoding.UTF8); 
      _XMLSerial.Serialize(_StreamWriter, __Lista); 

      _StreamMem.Position = 0; 
      _XMLDoc.Load(_StreamMem); 
      if (_XMLDoc.ChildNodes.Count > 0) 
       _ListToXMLDocument = _XMLDoc; 
     } 
     catch (Exception __Excp) 
     { 
      new uException(__Excp).GerarLogErro(CtNomeBiblioteca); 
     } 

     return _ListToXMLDocument; 
    } 
    #endregion 

接收代碼:

#region XMLDocumentToTypedList 
    private List<T> XMLDocumentToTypedList<T>(string __XMLDocument) 
    { 
     List<T> _XMLDocumentToTypedList = new List<T>(); 

     try 
     { 
      XmlSerializer _XMLSerial = new XmlSerializer(typeof(List<T>)); 
      MemoryStream _MemStream = new MemoryStream(); 

      StreamWriter _StreamWriter = new StreamWriter(_MemStream, Encoding.UTF8); 
      _StreamWriter.Write(__XMLDocument); 

      _MemStream.Position = 0; 
      _XMLDocumentToTypedList = (List<T>)_XMLSerial.Deserialize(_MemStream); 
      return _XMLDocumentToTypedList; 
     } 
     catch (Exception _Ex) 
     { 
      new uException(_Ex).GerarLogErro(CtNomeBiblioteca); 
      throw _Ex; 
     } 
    } 
    #endregion 
+0

如果您按順序一起調用這些方法,它們是否有效? (也就是說,你是積極的,這兩個methoods存在的問題,而不是網絡服務電話?) – 2010-06-17 12:47:12

+0

感謝哥們,但我設法解決這個問題。謝謝您的幫助。 – 2010-06-17 14:38:53

回答

0

經過一番努力,並檢查從互聯網上一些代碼,我設法解決這個問題。 但是,我不知道是什麼原因,但知道這種方式有效。在代碼下面。

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.LoadXml(__XMLDocument); 

StringWriter stringWriter = new StringWriter(); 
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter); 
xmlDoc.WriteTo(xmlWriter); 

XmlSerializer _XMLSerial = new XmlSerializer(typeof(List<tinfCte>)); 
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(stringWriter.ToString())); 

stream.Position = 0; 
_XMLDocumentToTInfCTeList = (List<tinfCte>)_XMLSerial.Deserialize(stream); 
return _XMLDocumentToTInfCTeList;