C# 如何解析有序的txt文件
我在下面有一个文本文件。我试图解析文本文件并存储值,但我不确定最好的方法是什么。我现在让它逐行读取,并使用regex查找文件中的某些文本。我想以这种方式存储变量。我该怎么做C# 如何解析有序的txt文件,c#,parsing,C#,Parsing,我在下面有一个文本文件。我试图解析文本文件并存储值,但我不确定最好的方法是什么。我现在让它逐行读取,并使用regex查找文件中的某些文本。我想以这种方式存储变量。我该怎么做 -Table |-Element # (iteration) |-Attribute Name |-Attribute Value -Table |-Element # (iteration)
-Table
|-Element # (iteration)
|-Attribute Name
|-Attribute Value
-Table
|-Element # (iteration)
|-Attribute Name
|-Attribute Value
表:FXsegments
元素段:
属性版本:910004000
日期:1975-08-20
属性sbdate类型:YMD
属性XMLversion:V_R072518_1514
属性XMLcrc:1970222190
属性类型:I
属性脚本:NOSCRIPT
元素段:
属性一:JAYESM
属性中间:W
属性last:CHOKSI
属性后缀:SR
属性日期报告:2018-10-17
属性daterep类型:YMD
属性欺诈:N
属性文件号:F
属性filehit:1
表:部分
元素段:
属性产品代码:07000
属性文件号:1
属性文件命中:
属性版本:0
属性国家:1
属性语言:1
`
你到底想要什么?要重写文本文件吗?还是要将数据存储到其他地方?抱歉。我应该澄清一下。我试图编写一个循环,逐行读取文本文件,并以上述方式存储它们。不确定如何处理此问题以及使用什么数据类型
while ((line = streamReader.ReadLine()) != null)
{
Regex regexSegName = new Regex(".*FXsegments.*|.*PXsegments.*;
Match matchSegName = regexSegName.Match(line);
if (matchSegName.Success)
{
Console.WriteLine(line);
}
Regex regexSegNameITR = new Regex("Element .*");
Match matchSegNameITR = regexSegNameITR.Match(line);
if (matchSegNameITR.Success)
{
Console.WriteLine(line);
}
Regex regexSegAttr= new Regex("Attribute .*");
Match matchSegAttr = regexSegAttr.Match(line);
if (matchSegAttr.Success)
{
string attributeLine = Regex.Replace(line, "Attribute ", "");
string[] parsed = attributeLine.Split(':');
Console.WriteLine(parsed[0] + " " + parsed[1]);
}
}