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

发布于 2024-09-06 12:12:20 字数 1906 浏览 3 评论 0原文

同志们,

我遇到以下问题。捕获列表结构、序列化(有效 W3C)并发送到 Web 服务。在我收到的WebService中,转换为字符串,由W3C和反序列化器有效,但当我尝试运行它时,总是出现错误,说某些对象未关闭。

有什么帮助吗?

发送代码:

#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

Camarades,

I'm having the following problem. Caught a list Struct, Serialize (Valid W3C) and send to a WebService. In the WebService I receive, transform to a string, valid by the W3C and then Deserializer, but when I try to run it, always occurs error, saying that some objects were not closed.

Any help?

Sent Code:

#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

Receive Code:

    #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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2024-09-13 12:12:20

经过一番努力,并检查了互联网上的一些代码,我设法解决了这个问题。
我不知道什么原因,但是,知道这种方式有效。下面的代码。

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;

After much effort, and to examine some code from the internet, I managed to solve the problem.
I do not know what reason, however, know that this way works. Below the code.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文