【发布时间】:2014-02-11 23:13:05
【问题描述】:
我有以下“异常层次结构”
Exception one = new ArithmeticException("Numbers are yucky");
Exception two = new System.IO.FileNotFoundException("Files stinks", one);
Exception three = new ArgumentOutOfRangeException("Arguments hurt", two);
我正在尝试创建以下 xml..这是我现有的代码(我明白为什么它没有给我预期的结果)
XDocument returnDoc = new XDocument();
XElement root = new XElement("root");
if (null != three)
{
XElement exceptionElement = new XElement("Exception");
Exception exc = ex;
while (null != exc)
{
exceptionElement.Add(new XElement("Message", exc.Message));
exc = exc.InnerException;
}
root.Add(exceptionElement);
}
returnDoc.Add(root);
我得到了这个 xml:
<root>
<Exception>
<Message>Arguments hurt</Message>
<Message>Files stinks</Message>
<Message>Numbers are yucky</Message>
</Exception>
</root>
我正在尝试获取这个 Xml...
<root>
<Exception>
<Message>Arguments hurt</Message>
<Exception>
<Message>Files stinks</Message>
<Exception>
<Message>Numbers are yucky</Message>
</Exception>
</Exception>
</Exception>
</root>
“嵌套”异常的数量未知....可能是 1 到 N。
我无法让“递归 XElement”工作。
【问题讨论】:
标签: c# .net xml linq-to-xml