我想从 EventLogRecord.ToXml() 读取 xml 并以“友好”的方式打印输出,就像在 Windows 事件查看器中一样。输入:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Microsoft-Windows-Security-SPP" Guid="{E23B33B0-C8C9-472C-A5F9-F2BDFEA0F156}" EventSourceName="Software Protection Platform Service" />
<EventID Qualifiers="16384">8230</EventID>
<Version>0</Version>
<Level>4</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2013-07-12T07:46:16.000000000Z" />
<EventRecordID>11381</EventRecordID>
<Correlation />
<Execution ProcessID="0" ThreadID="0" />
<Channel>Application</Channel>
<Computer>test</Computer>
<Security />
</System>
<EventData>
<Data>Security-SPP-Action-StateData </Data>
</EventData>
输出:
System
Provider
[ Name] Microsoft-Windows-Security-SPP
[ Guid] {E23B33B0-C8C9-472C-A5F9-F2BDFEA0F156}
[ EventSourceName] Software Protection Platform Service
EventID 8230
[ Qualifiers] 16384
Version 0
Level 4
Task 0
Opcode 0
Keywords 0x80000000000000
TimeCreated
[ SystemTime] 2013-07-12T07:46:16.000000000Z
EventRecordID 11381
Correlation
Execution
[ ProcessID] 0
[ ThreadID] 0
Channel Application
Computer test
Security
EventData
Security-SPP-Action-StateData
我知道我可以使用 XmlTextReader 并在之后进行一些格式化,比如
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write(reader.Name);
while (reader.MoveToNextAttribute()) // Read the attributes.
Console.WriteLine(reader.Name + " " + reader.Value + " ");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(reader.Value);
break;
}
}
但是有没有更简单的方法用 Linq to XML 来实现呢?