如何在C#中编写自定义解析器来解析由自定义标记格式化的TEXT文件不使用XML/第三方Libs
[product]
*name*Name1*/name*
*cost*100*/cost*
[/product]
[product]
*name*Name2*/name*
*cost*120*/cost*
[/product]
...
我试图以一种可以将"Name1"one_answers"Name2"存储在字符串等中的方式来解析它。
这里有一个技巧性的方法。
让我给你一些关于这个非常的破解解决方案的一般指南。
我发现您的格式与XML非常相似。因此,您可以循环遍历字符串,并保留一个用true
初始化的布尔变量flag
。
For each character in the string,
if it is a `[`, replace it with `<`.
If it is a `]`, replace it with `>`.
If it is a `*`, check the flag.
If the flag is true, replace it with `<`.
If the flag is false, replace it with a `>`.
Then, flag = !flag;
现在您有了一个xml表示。您可以使用.NET Framework中内置的XML解析器对其进行解析。未使用第三方库!输入文本仍然是原始格式,没有更改为XML!
满足要求!