C# 使用控制台应用程序分析Elmah错误日志

C# 使用控制台应用程序分析Elmah错误日志,c#,xml,linq,xml-parsing,C#,Xml,Linq,Xml Parsing,为了尽量减少阅读错误日志的时间,我创建了以下小插件,它将解析Elmah错误日志中包含的相关信息,并最终生成Excel电子表格。目前我正在使用WriteLine进行测试。我面临的问题是,在第二个foreach循环中,我在每个node.attribute项中看到一个null引用异常。此代码的预期结果是从每个XML文档的标记中生成一个“属性”值列表 using System; using System.Collections.Generic; using System.IO; using System

为了尽量减少阅读错误日志的时间,我创建了以下小插件,它将解析Elmah错误日志中包含的相关信息,并最终生成Excel电子表格。目前我正在使用WriteLine进行测试。我面临的问题是,在第二个
foreach
循环中,我在每个node.attribute项中看到一个null引用异常。此代码的预期结果是从每个XML文档的
标记中生成一个“属性”值列表

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);
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Xml;
命名空间优化自动化
{
公共课程
{
公共静态void Main(字符串[]args)
{
DirectoryInfo异常=新的DirectoryInfo(“C:\\ErrorsMay2017”);
FileInfo[]ExceptionFiles=Exceptions.GetFiles(“*.xml”);
foreach(异常文件中的var异常)
{
字符串xml=”“;
XmlDocument doc=新的XmlDocument();
doc.LoadXml(xml);
foreach(doc.SelectNodes(//错误)中的XmlNode节点)
{
字符串errorId=node.Attributes[“errorId”].Value;
字符串类型=节点.属性[“类型”].值;
字符串消息=节点。属性[“消息”]。值;
字符串时间=节点。属性[“时间”]。值;
WriteLine(“{0}-{1}-{2}={3}”,errorId,类型,消息,时间);
}
}
}
}
}
我的问题是,为什么此时应该提取和解析的日志不会接收属性值


编辑1:仔细检查后,XmlNode节点值返回时没有“HasAttributes”

string xml=”“
应替换为
stringxml=File.ReadAllText(exception.FullName))

字符串xml=”“
应替换为
stringxml=File.ReadAllText(exception.FullName))@JamesCurran,效果不错。我可以请你将它设置为答案,这样我就可以接受它了吗?我希望你说“这一行只是示例。我用我的真实代码阅读文件”。然后我想说“然后用文件中的实际片段替换”