开发者

How do you serialize an exception (with nested exceptions) using SOAP Formatter?

Editor controls missing from IE again... Per this topic Simple C# Data Algorithms Question - Populate class from Exception class, at the bottom it was suggested that I use the .NET SOAPFormatter to serialize a nested tree of exceptions. How? Can anyone show me some code to do this (especially to walk the nested list of in开发者_高级运维ner exceptions)? I need to see the actual code not just an API suggestion please.

I can't seem to respond to the answer or get the editor controls in my locked down IE 8, but I would like to see, in the code, a walk of the nested exceptions by travsering the InnerException property and the exception added. I would like to see a MemoryStream used and the SOAPformatter.


This should make the point:

    private static void BinaryFormatterDemo()
    {

        // serialise 
        Exception ex = new Exception("Some message",
            new Exception("Another message"));
        Console.WriteLine(ex);
        BinaryFormatter bf = new BinaryFormatter();
        FileStream fs = new FileStream("ex.bin", FileMode.Create);
        bf.Serialize(fs, ex);
        fs.Close();


        // deserialise
        fs = new FileStream("ex.bin", FileMode.Open);
        Exception loadedEx = (Exception) bf.Deserialize(fs);
        Console.WriteLine(loadedEx);
        fs.Close();


    }

SoapFormatter is no different, just change BinaryFormatter with SoapFormatter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜