我一直注意到以下代码段不适用于大文件(我认为附加到 paneContent 字符串很慢):
string paneContent = String.Empty;
bool lineFound = false;
foreach (string line in File.ReadAllLines(path))
{
if (line.Contains(tag))
{
lineFound = !lineFound;
}
else
{
if (lineFound)
{
paneContent += line;
}
}
}
using (TextReader reader = new StringReader(paneContent))
{
data = (PaneData)(serializer.Deserialize(reader));
}
加快这一切的最佳方法是什么?我有一个看起来像这样的文件(所以我想获取两个不同标签之间的所有内容,然后反序列化所有这些内容):
A line with some tag
A line with content I want to get into a single stream or string
A line with content I want to get into a single stream or string
A line with content I want to get into a single stream or string
A line with content I want to get into a single stream or string
A line with content I want to get into a single stream or string
A line with some tag
注意:这些标签不是 XML 标签。