XMLWriterTraceListener生成文本墙而不是格式化的XML

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XMLWriterTraceListener生成文本墙而不是格式化的XML相关的知识,希望对你有一定的参考价值。

我有一个XMLWriterTraceListener对象添加到我的跟踪侦听器,像这样,

System.Diagnostics.XmlWriterTraceListener xmlTrace = new 
    System.Diagnostics.XmlWriterTraceListener("Trace.xml");
xmlTrace.IndentLevel = 1;
xmlTrace.IndentSize = 4;

System.Diagnostics.Trace.Listeners.Add(xmlTrace);

它成功地从Trace.WriteLine(),Trace.TraceInformation()等接收消息。唯一的问题是它写入的Trace.xml文件是人类不可读的,因为它不包含换行符或缩进。我错过了一个中间步骤(XMLStreamWriter?),它将提高输出?

当前输出显示:

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2014-03-20T18:05:43.2778822Z" /><Source Name="Program.vshost.exe" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Program.vshost" ProcessID="6840" ThreadID="9" /><Channel/><Computer>ODYSSEY</Computer></System><ApplicationData>Startup!</ApplicationData></E2ETraceEvent>

但我想读它:

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <EventID>0</EventID>
        <Type>3</Type>
        <SubType Name="Information">0</SubType>
        <Level>8</Level>
        <TimeCreated SystemTime="2014-03-20T18:05:43.2778822Z" />
        <Source Name="Program.vshost.exe" />
        <Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" />
        <Execution ProcessName="Program.vshost" ProcessID="6840" ThreadID="9" />
        <Channel/>
        <Computer>ODYSSEY</Computer>
    </System>
    <ApplicationData>Startup!</ApplicationData>
</E2ETraceEvent>
答案

看起来没有办法做到这一点,甚至没有反射:http://dotnetinside.com/en/framework/v4.0.30319/System/XmlWriterTraceListener

如果你可以在单声道版本中获得xml writer设置,你可能能够:http://dotnetinside.com/en/framework/Mono+Framework/System/XmlWriterTraceListener

您可以使用服务跟踪查看器打开XML跟踪:http://msdn.microsoft.com/en-us/library/ms732023%28v=vs.110%29.aspx

否则,复制并粘贴到您喜欢的IDE或XML编辑器。

另一答案

有点晚了但是如果你想在代码中这样做,下面的链接很好地展示了我们如何阅读XML。您可以阅读子树,然后可能反序列化为一个类。然后使用适当的设置将其序列化回XML。

我还从E2ETraceEvent Xml节点创建了C#类,以便反序列化Xml片段。

http://haishibai.blogspot.in/2009/04/monitoring-and-reading-svclog-file.html

示例代码:

using (var fileStream = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (XmlReader reader = new XmlTextReader(fileStream, XmlNodeType.Element, null))
{
    while (reader.Read())
    {
        if (reader.Name == "E2ETraceEvent" && reader.NodeType == XmlNodeType.Element)
        {
            using (XmlReader subReader = reader.ReadSubtree())
            {
                while (subReader.Read())
                {
                    var elementString = subReader.ReadOuterXml();
                    if (!string.IsNullOrWhiteSpace(elementString))
                    {
                        using (var xStream = this.GenerateStreamFromString(elementString))
                        {
                            XElement traceElement = XElement.Load(xStream);
                            var serializer = new XmlSerializer(typeof(E2ETraceEvent));
                            var traceObject = (E2ETraceEvent)serializer.Deserialize(traceElement.CreateReader());

                            var formattedXml = this.SerializeToXmlString<E2ETraceEvent>(traceObject); 
                        }
                    }
                }
            }
        }
    }
}
====================================

private Stream GenerateStreamFromString(string s)
{
    var stream = new MemoryStream();
    var writer = new StreamWriter(stream);
    writer.Write(s);
    writer.Flush();
    stream.Position = 0;
    return stream;
}
=====================================

private string SerializeToXmlString<T>(T objectToSerialize)
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    var settings = new XmlWriterSettings
    {
        Indent = true,
        OmitXmlDeclaration = true
    };
    var xml = string.Empty;

    using (var sw = new StringWriter())
    {
        using (XmlWriter writer = XmlWriter.Create(sw, settings))
        {
            xmlSerializer.Serialize(writer, objectToSerialize);
            xml = sw.ToString();
        }
    }

    return xml;
}
======================================

希望这可以帮助!

以上是关于XMLWriterTraceListener生成文本墙而不是格式化的XML的主要内容,如果未能解决你的问题,请参考以下文章

获取动态生成的文本区域的值

利用JS生成博文目录及CSS定制博客

在图像文本文档中随机生成合成噪声

2022生成模型进展有多快?新论文盘点9类生成模型代表作

c#中输入文本文字,将输入的文字生成图片

快速生成较大文本文档的两种方法