none
Parser issue ( derictory aternitive ) duplicate value issue. RRS feed

  • Question

  • Hello all thank you for taking your time to read my post i have a issue little one i want to read a file and seprate it into "nameherev" "valuehere"

    only issue is that the file has dublicates as some values just have ""  but i not sure how to work around this i want to get all value and pairs 

    and print them to screen in text boxes then change values and save back only issue i have here this 

    this is the error and this is my code i have done 

    private String header;
            private Dictionary<String, String> tokens;
            private String file;
    
    
            string weapon = @"C:\Users\Martin\Downloads\wpn_t7_loot_pistol_m1911.gdt";
    
    
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
    
    
            public void Parser(String file)
            {
    
                this.file = file;
                String plainText;
                using (StreamReader sr = new StreamReader(file))
                {
                    plainText = sr.ReadToEnd();
                }
    
                String[] tokens = plainText.Split('"');
                this.tokens = new Dictionary<string, string>(tokens.Length + 10);
    
                header = tokens[0];
                for (int i = 1; i < tokens.Length; i += 2)
                {
                    String key = tokens[i];
                    String val = (i + 1 <= tokens.Length) ? tokens[i + 1] : "";
    
                    this.tokens.Add(key, val);
                }
    
    
            }
    
            public void test()
            {
                textBox1.Text = Search("activate_mode");
            }
    
            public String Search(String name)
            {
                foreach (KeyValuePair<String, String> pair in tokens)
                {
                    if (pair.Key == name)
                        return pair.Value;
                }
    
                return null;
            }
    
            public void Set(String key, String val)
            {
                tokens[key] = val;
            }
    
            public void Save(String file)
            {
                using (StreamWriter sw = new StreamWriter(File.OpenWrite(file)))
                {
                    sw.Write(header);
                    foreach (KeyValuePair<String, String> pair in tokens)
                    {
                        sw.Write("" + pair.Key + "" + pair.Value);
                    }
                }
            }
    
    
            public void Save()
            {
                this.Save(file);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Parser(weapon);
                test();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Set("displayName", textBox1.Text);
                Save(weapon);
            }
        }

    and this is the file i am trying to read.

    file download

    i want to get all the values in btween the braces so for example 

    	"camo_t7_m1911" ( "weaponcamotable.gdf" )
    	{
    		"configstringFileType" "WEAPONCAMOTABLE"
    		"numCamoTables" "3"
    		"table_01_name" "camo_t7_m1911_ship"
    		"table_02_name" "camo_t7_m1911_dlc1_base76"
    		"table_03_name" "camo_t7_m1911_dlc2_base121"
    		"table_04_name" ""
    		"table_05_name" ""
    		"table_06_name" ""
    		"table_07_name" ""
    		"table_08_name" ""
    		"table_09_name" ""
    		"table_10_name" ""
    	}

    thats only the top of the file but all the stuff btween the 

    {

    }

    and that goes for whole file only want the values to show "name" "value"but i dont know how to get round dubplicates any help would be much appriated i been working on this for like a day now not sure how to fix this. 

    thanks elfenliedtopfan5 

    Wednesday, November 15, 2017 1:00 AM

Answers

  • Hello,

     The One Drive link to my Class Parser was designed on the idea

    that your weapon data files contained structures that described

    weapons as Property objects, an item and item properties.

     

     When you use get and/or set without any identifier it's called

      Auto Implemented Properties <- click link for MSDN Docs page

    for further reading.  Basic idea is giving properties to describe

    an objects. Example: Apple and Orange are Fruit, with skin,

    seeds but not same color, size and taste.

     

     If you have any more question not related to your Original Post

    please make a new posting.  If you want to use my Parser class

    in your project then I can help you implement as smooth as

    possible. 

     Thanks :)

    Thursday, November 16, 2017 10:10 PM

All replies

  • Hello elfenliedtopfan5,

    Because your file are not created based on json or xml format, the file's processing will become a litter difficult. I suggest you parse the file line by line and take out the useful information. And I create a simple demo base on your description and it works well on my side. Because your file contains the same key as below. so I use List<KeyValuePair<string, string>> object instead of Dictionary object.

    The code

      private List<KeyValuePair<string, string>> tokens1;
            public void Parse2(string File) {
                this.file = File;
                string plainText;
                string previewLine="";
                //this.tokens = new Dictionary<string, string>();
                this.tokens1 = new List<KeyValuePair<string, string>>();
                using (StreamReader sr = new StreamReader(file)) {
                    //skip the first line of file
                    sr.ReadLine();
                    while ((plainText = sr.ReadLine()) != null)
                    {
                        String line = plainText;
                        if (line.Contains("{"))
                        {
                            tokens1.Add(new KeyValuePair<string, string>(previewLine,"Header"));
                        }
                        else {
                            String[] key_Value = plainText.Split(' ');
                            if (key_Value.Length == 2)
                            {
                                tokens1.Add(new KeyValuePair<string, string>( key_Value[0], key_Value[1]));
                            }
                        }
                       if (plainText.Contains("}")) {
                           
                        }
                        previewLine = line;
                    }
                }
            }

    If you have any issues with my reply please feel free to contact me.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 15, 2017 7:15 AM
    Moderator
  • Hello elfenliedtopfan5,

    Because your file are not created based on json or xml format, the file's processing will become a litter difficult. I suggest you parse the file line by line and take out the useful information. And I create a simple demo base on your description and it works well on my side. Because your file contains the same key as below. so I use List<KeyValuePair<string, string>> object instead of Dictionary object.

    The code

      private List<KeyValuePair<string, string>> tokens1;
            public void Parse2(string File) {
                this.file = File;
                string plainText;
                string previewLine="";
                //this.tokens = new Dictionary<string, string>();
                this.tokens1 = new List<KeyValuePair<string, string>>();
                using (StreamReader sr = new StreamReader(file)) {
                    //skip the first line of file
                    sr.ReadLine();
                    while ((plainText = sr.ReadLine()) != null)
                    {
                        String line = plainText;
                        if (line.Contains("{"))
                        {
                            tokens1.Add(new KeyValuePair<string, string>(previewLine,"Header"));
                        }
                        else {
                            String[] key_Value = plainText.Split(' ');
                            if (key_Value.Length == 2)
                            {
                                tokens1.Add(new KeyValuePair<string, string>( key_Value[0], key_Value[1]));
                            }
                        }
                       if (plainText.Contains("}")) {
                           
                        }
                        previewLine = line;
                    }
                }
            }

    If you have any issues with my reply please feel free to contact me.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    thank you ever so much for your reply only issue i have with this is i want to show a certin part to textbox's this is how i set it 

            public void Set(String key, String val)
            {
                tokens1[key] = val;
            }

    but when i change this it gives me this error. 

    Argument 1: cannot convert from 'string' to 'int'

    and this is how i loaded it before when i was using directory 

    Set("activate_mode", textEdit1.Text);

    but i get a error above and im not sure how to fix that so i could look for the key and display the value 

    as set function looks for the key aka ( active mode ) and displays the value to the textbox. 

    sorry to be a pain elfenliedtopfan5

    Wednesday, November 15, 2017 6:18 PM

  •         public void Set(String key, String val)
            {
                tokens1[key] = val;
            }

    but when i change this it gives me this error. 

    Argument 1: cannot convert from 'string' to 'int'


    That is because tokens1 is now a list of KeyValuePairs<String,String>, not a dictionary anymore.


    My Technet Articles

    If you like this or another reply, vote it up!
    If you think this or another reply answers the original question, mark it or propose it as an answer.


    Mauricio Feijo
    www.mauriciofeijo.com

    Wednesday, November 15, 2017 6:45 PM

  •         public void Set(String key, String val)
            {
                tokens1[key] = val;
            }

    but when i change this it gives me this error. 

    Argument 1: cannot convert from 'string' to 'int'


    That is because tokens1 is now a list of KeyValuePairs<String,String>, not a dictionary anymore.


    My Technet Articles

    If you like this or another reply, vote it up!
    If you think this or another reply answers the original question, mark it or propose it as an answer.


    Mauricio Feijo
    www.mauriciofeijo.com

    Yeah i some how managed to get it to load the file via the search i did instead ;) 

    i just have 2 issues i kinda understood directorys only issues i have now is this

    this is how it looks in file 

    "displayName" "A99"
     

    and this is what it does when it stores it in list file for some reason. 

    "\t\t\"displayName\""

    plus without directory i dont know how to save back to the file i have the search working using it like the above.

    gif

    only issue i have now is i need to save it back to file if i edit the values as shown in gif above if i save out the file and changes how would i go to doing that with a listkeyvaluepair as it stands to get the values shown above i have this code here

            public void test()
            {
                textEdit4.Text = Search("\t\t\"displayName\"");
                textEdit1.Text = Search("\t\t\"activate_mode\"");
                textEdit2.Text = Search("\t\t\"activate_with_primed_delay\"");
                textEdit3.Text = Search("\t\t\"activatesSlam\"");
            }
    
            public String Search(String name)
            {
                foreach (KeyValuePair<String, String> pair in tokens1)
                {
                    if (pair.Key == name)
                        return pair.Value;
                }
    
                return null;
            }
    witch only shows the values saving them im not sure how that will work but it will mess up if i save back to the file with the t/t/name/ it has to be just "name" "value" if that makes any sense 

    Wednesday, November 15, 2017 7:14 PM
  • Hello,

     If you are interested, perhaps the code on my One Drive will be useful.

     

     https://1drv.ms/u/s!AnZy9F1PKht-gQFh5Xifbj25eiaZ

     

     I used VS2012 and Console app project on Win7.  I drafted a

    functional class parser base on your project file sample. Because

    your project is Win Forms, I figured breaking the parser into smaller

    steps would be helpful. Take a look at the code, use it, get inspired,

    etc.  No IP worries. Remember,  its bare bones but plenty of room

    for potential.

     

     Good Luck :)

    Thursday, November 16, 2017 8:17 AM
  • Hello elfenliedtopfan2,

    If you want to change line value and then save it to file, This also become a litter complicated because your file doesn't take common text format like josn or xml.

    In my knowledge, you must read all context file and rewrite a line ,save the entire line to original file. Beside this I can't came out another approach. The Following is some simple demo provided for your reference.

    https://stackoverflow.com/questions/1971008/edit-a-specific-line-of-a-text-file-in-c-sharp

    For example.

    string text = File.ReadAllText("test.txt");
    text = text.Replace("some text", "new value");
    File.WriteAllText("test.txt", text);

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 16, 2017 10:37 AM
    Moderator
  • Hello elfenliedtopfan2,

    If you want to change line value and then save it to file, This also become a litter complicated because your file doesn't take common text format like josn or xml.

    In my knowledge, you must read all context file and rewrite a line ,save the entire line to original file. Beside this I can't came out another approach. The Following is some simple demo provided for your reference.

    https://stackoverflow.com/questions/1971008/edit-a-specific-line-of-a-text-file-in-c-sharp

    For example.

    string text = File.ReadAllText("test.txt");
    text = text.Replace("some text", "new value");
    File.WriteAllText("test.txt", text);

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    i have tried it but it dont save the text in the box back to the file its self only issue i have now is when i do save back it does not save right it saves all on one line and removes the { top brace and makes the file not readable to the game its just a shame directory dont work for this as the setup for that was so much easier and you could edit the values in and save it out 
    Thursday, November 16, 2017 8:12 PM
  • Hello,

     If you are interested, perhaps the code on my One Drive will be useful.

     

     https://1drv.ms/u/s!AnZy9F1PKht-gQFh5Xifbj25eiaZ

     

     I used VS2012 and Console app project on Win7.  I drafted a

    functional class parser base on your project file sample. Because

    your project is Win Forms, I figured breaking the parser into smaller

    steps would be helpful. Take a look at the code, use it, get inspired,

    etc.  No IP worries. Remember,  its bare bones but plenty of room

    for potential.

     

     Good Luck :)

    Yeah im quite new to this so im not sure how that works looks intresting kinda like setting a bool with {get;set;}

    but i don't really know what or how to implement that right as my skill level is not great with list.

    Thursday, November 16, 2017 8:14 PM
  • Hello,

     The One Drive link to my Class Parser was designed on the idea

    that your weapon data files contained structures that described

    weapons as Property objects, an item and item properties.

     

     When you use get and/or set without any identifier it's called

      Auto Implemented Properties <- click link for MSDN Docs page

    for further reading.  Basic idea is giving properties to describe

    an objects. Example: Apple and Orange are Fruit, with skin,

    seeds but not same color, size and taste.

     

     If you have any more question not related to your Original Post

    please make a new posting.  If you want to use my Parser class

    in your project then I can help you implement as smooth as

    possible. 

     Thanks :)

    Thursday, November 16, 2017 10:10 PM
  • hello elfenliedtopfan2,

    The "save" question is no more relating with "Parse" problems. If the original problem has been solved,  you could mark the reply as answer. This can be beneficial to other community members reading this thread.

    For The new issues , you could repost a thread in C# forum. This could attract more people to get involved and also we can provide more suitable support with clear purpose. 

    Sincerely,

    Fei Hu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 17, 2017 10:58 AM
    Moderator