0
为了尽量减少读取错误日志所用的时间,我创建了以下小插件,它将解析Elmah错误日志中包含的相关信息,并最终生成Excel电子表格。目前我正在使用WriteLine进行测试。我面临的问题是,在第二个foreach
循环内,我在每个node.attribute项目中都看到一个空引用异常。此代码的预期结果是从每个XML文档的<error>
标记中生成一个“属性”值列表。使用控制台应用程序解析Elmah错误日志
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
namespace ExcelSortingAutomation
{
public class Program
{
public static void Main(string[] args)
{
DirectoryInfo Exceptions = new DirectoryInfo("C:\\ErrorsMay2017");
FileInfo[] ExceptionFiles = Exceptions.GetFiles("*.xml");
foreach (var exception in ExceptionFiles)
{
string xml = "<error></error>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode node in doc.SelectNodes("//error"))
{
string errorId = node.Attributes["errorId"].Value;
string type = node.Attributes["type"].Value;
string message = node.Attributes["message"].Value;
string time = node.Attributes["time"].Value;
Console.WriteLine("{0} - {1} - {2} = {3}", errorId, type, message, time);
}
}
}
}
}
我的问题是,为什么日志,这应该由这点拉出并解析,不接受属性值?
编辑1:经仔细检查,XmlNode的节点值被返回没有 “HasAttributes”
线'串XML被替换= “ ”;'应替换为'string xml = File.ReadAllText(exception.FullName));' –
@JamesCurran这很好。请允许我请您将其设置为答案,以便我可以接受它? – jpears
我期待着你说“这条线只是为了这个例子,我用真实的代码阅读了这个文件”。然后我会说“然后用文件中的一些实际片段替换 ” –