C# Linq转换为xml以读取windows事件日志xml
我想从EventLogRecord.ToXml()读取xml,并以“友好”的方式打印输出,就像在Windows事件查看器中一样。输入:C# Linq转换为xml以读取windows事件日志xml,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我想从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-F2BDFEA
<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>
我知道我可以使用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到XML的方法来实现这一点?可能重复@spender谢谢!这真是一个复制品。
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;
}
}