简体   繁体   English

使用C#将日志文件解析为XML

[英]Parsing Log File into XML using C#

I need to separate the information below and only take the XML out, I'm trying to figure out the most efficient way to do this. 我需要将下面的信息分开,仅取出XML,我试图找出最有效的方法。 I'm not sure what approach to start with in regards to removing the first 3 lines, and getting the XML DTD. 对于删除前三行并获取XML DTD,我不确定从什么开始。 In my head I was thinking that the best approach to this would be to ignore/remove the 3 lines until XML tags are opened with '<' but I wasn't sure if I should put that in a giant string? 在我的脑海中,我想最好的方法是忽略/删除3行,直到用'<'打开XML标签,但是我不确定是否应该将它放在一个巨大的字符串中? Honestly anything would be helpful, I'm stuck figuring this bad boy out, and I'm sure it's not going to be as hard as I'm making it out to be, but I am stuck. 老实说,任何事情都会有所帮助,我一直在努力找出这个坏男孩,而且我确信它不会像我要表现出来的那样艰难,但是我被困住了。 Thank you very much! 非常感谢你!

EDIT: This is a .log file 编辑:这是一个.log文件

This is the sample Text Document: 这是示例文本文档:

VCS (1.0.11.111): [10/9/2015 12:00:02 AM]
POST https://ex.sample.com/samp/x/sample
Content-Type: application/x-www-form-urlencoded
<?xml version="1.0" encoding="UTF-8"?>
    <command name="sample name_" signature="some stuff" address="sample.com">
    <param name="CurrentVersion">1111</param>
    <param name="MotherboardName">Dell Inc. PowerEdge R420</param>
</command>
HTTP/1.1 200 OK

The easiest way would be to get the first index of < and the last index of > substring your file and let the .Net Xml Parser do its work. 最简单的方法是获取文件的<和第一个索引>子字符串,然后让.Net Xml Parser进行工作。

But I am not sure if it is the fastest way. 但是我不确定这是否是最快的方法。

XML Parsing to class has been answered here XML解析到类已在这里回答

What about using some regular expression? 使用一些正则表达式呢? Try this: 尝试这个:

        var regex = new Regex(@"<\?xml.*\?>(?<Xml>.*)HTTP/", RegexOptions.Singleline);

        var match = regex.Match(inputString);

        if (match.Success)
        {
            var xmlResult = match.Groups["Xml"].Value;
        }

You will have all xml in the variable xmlResult . 您将在变量xmlResult拥有所有xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM