2

我有来自服务器的以下日志文​​件,我想从以下字符串中提取 xml。

2:00:11 PM >>Response: <?xml version="1.0" encoding="UTF-8"?>

<HotelML xmlns="http://www.xpegs.com/v2001Q3/HotelML"><Head><Route Destination="TR" Source="00"><Operation Action="Create" App="UltraDirect-d1c1_" AppVer="V1_1" DataPath="/HotelML" StartTime="2013-07-31T08:33:13.223+00:00" Success="true" TotalProcessTime="711"/></Route>............

</HotelML>


3:00:11 PM >>Response: <?xml version="1.0" encoding="UTF-8"?>

<HotelML xmlns="http://www.xpegs.com/v2001Q3/HotelML"><Head><Route Destination="TR" Source="00"><Operation Action="Create" App="UltraDirect-d1c1_" AppVer="V1_1" DataPath="/HotelML" StartTime="2013-07-31T08:33:13.223+00:00" Success="true" TotalProcessTime="711"/></Route>............

</HotelML>

5:00:11 PM >>Response: <?xml version="1.0" encoding="UTF-8"?>

<HotelML xmlns="http://www.xpegs.com/v2001Q3/HotelML"><Head><Route Destination="TR" Source="00"><Operation Action="Create" App="UltraDirect-d1c1_" AppVer="V1_1" DataPath="/HotelML" StartTime="2013-07-31T08:33:13.223+00:00" Success="true" TotalProcessTime="711"/></Route>............

</HotelML>

我已经为相同的内容编写了以下正则表达式,但它仅匹配字符串中的第一个条目。但我想将所有 xml 字符串作为集合返回。

(?<= Response:).*>.*</.*?>
4

2 回答 2

2

你为什么不匹配 from <HotelMLto </HotelML

就像是:

<HotelML .*</HotelML>

或者,只需逐行浏览文件,只要找到匹配的行

^.* PM >>Response:.*$

将以 xml 格式读取以下行,直到下一个匹配行...

于 2013-07-31T11:46:51.933 回答
1

这是另一种方法,它应该给你一个List<XDocument>

using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {

        var input = File.ReadAllText("text.txt");
        var xmlDocuments = Regex
            .Matches(input, @"([0-9AMP: ]*>>Response: )")
            .Cast<Match>()
            .Select(match =>
                {
                    var currentPosition = match.Index + match.Length;
                    var nextMatch = match.NextMatch();
                    if (nextMatch.Success == true)
                    {
                        return input.Substring(currentPosition,
                            nextMatch.Index - currentPosition);
                    }
                    else
                    {
                        return input.Substring(currentPosition);
                    }
                })
            .Select(s => XDocument.Parse(s))
            .ToList();
    }
}
于 2013-07-31T12:16:22.860 回答