XML序列化和反序列化问题XML结构

时间:2010-06-17 12:20:24

标签: web-services xml-serialization c#-4.0 xml-deserialization

Camarades,

我遇到以下问题。捕获列表Struct,Serialize(Valid 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

1 个答案:

答案 0 :(得分: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;