MVVM get data from text file

I am unable to get the search logic in a text file and then get the data I need using the view model of the model view. Basically, I have to make a dictionary app and I have the word, language and description in a text file. How:

cat; e English; this is an animal with four legs

In the model, I have a text box where the client writes the word and two other boxes where the language and description of the word should be displayed.

I just can't seem to find how to search in this file. I tried to search the web, but nothing seemed to match my exact question.

+3


source to share


2 answers


If your file doesn't change, you can elude reading the entire file before running the app and put the data into model lists for your view models.

Since this is essentially a CSV file and assuming each record is a string, using a separator column as separator, we can use a .Net CSV parser to parse your file in your models:

Basic model:

public class DictionaryEntryModel {
    public string Word { get; set; }
    public string Language { get; set; }
    public string Description { get; set; }
}

      



An example of a view model with a constructor to populate your models:

public class DictionaryViewModel {

    // This will be a INotify based property in your VM
    public List<DictionaryEntryModel> DictionaryEntries { get; set; }

    public DictionaryViewModel () {
        DictionaryEntries = new List<DictionaryEntryModel>();

        // Create a parser with the [;] delimiter
        var textFieldParser = new TextFieldParser(new StringReader(File.ReadAllText(filePath)))
        {
            Delimiters = new string[] { ";" }
        };

        while (!textFieldParser.EndOfData)
        {
            var entry = textFieldParser.ReadFields();
            DictionaryEntries.Add(new DictionaryEntryModel()
                {
                    Word = entry[0],
                    Language = entry[1],
                    Description = entry[2]
                });
        }

        // Don't forget to close!
        textFieldParser.Close();
    }
}

      

Now you can bind your view using a property DictionaryEntries

and while your application is open it will save your complete file as a list DictionaryEntryModel

.

Hope this helps!

+1


source


I am not addressing the MVVM part here, but simply how to search a text file to get the resulting data according to the search term using a case insensitive regex.

string dictionaryFileName = @"C:\Test\SampleDictionary.txt"; // replace with your file path
string searchedTerm = "Cat"; // Replace with user input word

string searchRegex = string.Format("^(?<Term>{0});(?<Lang>[^;]*);(?<Desc>.*)$", searchedTerm);

string foundTerm;
string foundLanguage;
string foundDescription;

using (var s = new StreamReader(dictionaryFileName, Encoding.UTF8))
{
    string line;
    while ((line = s.ReadLine()) != null)
    {
        var matches = Regex.Match(line, searchRegex, RegexOptions.IgnoreCase);
        if (matches.Success)
        {
            foundTerm = matches.Groups["Term"].Value;
            foundLanguage = matches.Groups["Lang"].Value;
            foundDescription = matches.Groups["Desc"].Value;
            break;
        }
    }
}

      



Then you can display the resulting lines to the user.

Note that this will work for typical input words, but it may produce strange results if the user enters special characters that interfere with the regular expression syntax. In most cases, this can be fixed with Regex.Escape(searchedTerm)

.

0


source







All Articles