简体   繁体   中英

Parsing NON-XML syntax'ed TEXT file in C#

How does one write a custom parser in C# to parse a TEXT file that is formatted by custom tags? WITHOUT using XML / 3rd Party Libs

[product] 
*name*Name1*/name*
*cost*100*/cost*
[/product]
[product]
*name*Name2*/name*
*cost*120*/cost*
[/product]
...

I'm trying to parse it in a way that I can store "Name1" and "Name2" in strings, etc.

Here's a hacky way to do it.

Let me just give you some general guidelines on this very hacky solution.

I see that your format is very similar to XML. So you can just loop through the string and keep a boolean variable flag , initialised with true .

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;

Now you have an xml representation. And you can parse it with the XML parser that is built into the .NET Framework. No third party libraries used! The input text is still in the original format, not changed to XML!

REQUIREMENT MET!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM