如何使用C#解析下面提到的文件内容 [英] How to parse the below mentioned file content using C#

查看:54
本文介绍了如何使用C#解析下面提到的文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Record FileRecord "1" {
	Record LotRecord "abcd" {
		Field a 1{"NONE"}
		Field b 2 {2, 3}
		Field C 3 {"None", "a", "b"}
		Field d 1 {0}
		List ClassLookupList {
			Columns 3 { int32 CLASSNUMBER, string CLASSNAME, string CLASSCODE }
			Data 5{
				0 "aa" "";
				1 "bb" "";
				2 "cc" "";
				3 "dd" "";
				4 "ee" "";
			}
		}
		Record DataRecord "377" {
			Field a 2 {0, 0}
			Field b 1 {""}
			Field C 7 {"None", "", "", "", "", "", ""}
			List DefectList {
				Columns 14 { int32 DEFECTID, int32 XREL, int32 YREL, int32 XINDEX, int32 YINDEX, int32 XSIZE, int32 YSIZE, float DEFECTAREA, int32 DSIZE, int32 CLASSNUMBER, int32 TEST, int32 ROUGHBINNUMBER, int32 FINEBINNUMBER, int32 REVIEWSAMPLE }
				Data 3{
					1 134312000 133394000 0 0 0 0 0.0 758 0 6 0 0 0;
					2 150981000 115346000 0 0 0 0 0.0 299 0 6 0 0 0;
					3 194310000 137293000 0 0 0 0 0.0 300 0 6 0 0 1;				}
			}
			
	}
	Field FileTimestamp 2 {"09-10-2018", "09:28:46"}
}
EndOfFile;





我的尝试:





What I have tried:

using (var reader = new StreamReader(fileName))
				{
					string line = null;//await reader.ReadToEndAsync();
					while ((line = await reader.ReadLineAsync()) != null)
					{
						if (!string.IsNullOrWhiteSpace(line) || !string.IsNullOrWhiteSpace(line.Split(',')[0]))
						{
							if (line.Contains("1.8"))
								await GetData(reader.ReadLine());
							else
								return null;
						}
					}



				}





这种方法需要大量编码。有人可以用更好的方法帮助我吗?



This approach will take lot of coding. Could someone please help me with better approach?

推荐答案

了解你的数据。



你可以解析很快就会在内存中出现成千上万的文本行。



加载后,您可以进行多次扫描以获得您想要的内容;包括删除妨碍的东西。



List< string>优于sting阵列以获得性能和灵活性。



根据具体情况使用File.ReadAllText或RealAllLines。



使用Debug或Trace输出来监控解析。
Know your data.

You can parse "thousands" of lines of text in memory in no time at all.

Once loaded, you can do multiple scans to get at want you want; including "removing" stuff that "gets in the way".

List<string> is preferred over "sting arrays" for performance and flexibility.

Use File.ReadAllText or RealAllLines depending on the situation.

Use Debug or Trace output to monitor your parsing.


这篇关于如何使用C#解析下面提到的文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆