Sage.SageException.ConvertToXml C# (CSharp) Method

ConvertToXml() protected method

Saves the current instance as an XmlElement, using the specified ownerDocument to create the element.
protected ConvertToXml ( Exception instance, XmlDocument ownerDocument, ProblemInfo problemInfo = null ) : XmlElement
instance System.Exception The exception instance.
ownerDocument System.Xml.XmlDocument The document to use to create the element.
problemInfo ProblemInfo The problem info associated with the exception.
return System.Xml.XmlElement
        protected internal virtual XmlElement ConvertToXml(Exception instance, XmlDocument ownerDocument, ProblemInfo problemInfo = null)
        {
            XmlElement exceptionElement = ownerDocument.CreateElement("exception");
            exceptionElement.SetAttribute("type", instance.GetType().ToString());
            exceptionElement.SetAttribute("message", instance.Message);
            exceptionElement.SetAttribute("htmlDescription", instance.Message
                .Replace("<", "&lt;")
                .Replace(">", "&gt;")
                .Replace("\t", "&#160;&#160;&#160;&#160;")
                .Replace("\n", "<br/>"));

            XmlElement straceNode = (XmlElement) exceptionElement.AppendChild(ownerDocument.CreateElement("stacktrace"));
            string[] stackTrace = instance.StackTrace != null
                ? instance.StackTrace.Split(new[] { '\n' })
                : new[] { string.Empty };

            if (instance.GetType() == typeof(XmlException))
            {
                exceptionElement.SetAttribute("sourceuri", ((XmlException) instance).SourceUri);
                exceptionElement.SetAttribute("linenumber", ((XmlException) instance).LineNumber.ToString());
                exceptionElement.SetAttribute("lineposition", ((XmlException) instance).LinePosition.ToString());
            }

            foreach (string t in stackTrace)
            {
                XmlElement frameNode = (XmlElement) straceNode.AppendChild(ownerDocument.CreateElement("frame"));
                Match match;
                if ((match = Regex.Match(t, "^\\s*at (.*) in (.*):line (\\d+)[\\s\\r]*$")).Success)
                {
                    frameNode.SetAttribute("text", match.Groups[1].Value);
                    frameNode.SetAttribute("file", match.Groups[2].Value);
                    frameNode.SetAttribute("line", match.Groups[3].Value);
                }
                else
                {
                    frameNode.SetAttribute("text", Regex.Replace(t, "^\\s*at ", string.Empty));
                }
            }

            if (problemInfo != null && problemInfo.InfoBlocks.Count != 0)
            {
                foreach (KeyValuePair<string, IDictionary<string, string>> infoBlock in problemInfo.InfoBlocks)
                {
                    XmlElement blockElement = exceptionElement.AppendElement("infoblock");
                    blockElement.SetAttribute("name", infoBlock.Key);

                    foreach (string key in infoBlock.Value.Keys)
                    {
                        string value = infoBlock.Value[key];
                        XmlElement lineElement = blockElement.AppendElement("line");
                        if (key != value)
                            lineElement.SetAttribute("name", key);

                        lineElement.InnerText = value;
                    }
                }
            }

            return exceptionElement;
        }