Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVVM从文本文件获取数据_C#_File_Mvvm - Fatal编程技术网

C# MVVM从文本文件获取数据

C# MVVM从文本文件获取数据,c#,file,mvvm,C#,File,Mvvm,我无法获得如何在文本文件中搜索然后使用模型视图模型获取所需数据的逻辑。 基本上,我必须做一个字典应用程序,我在文本文件中有单词、语言和描述。比如: 猫;英语;它是一种四条腿的动物 在这个模型中,我有一个文本框,客户端在其中写入一个单词,还有两个其他框,其中应该显示单词的语言和描述 我就是不知道如何在这个文件中搜索。我试图在线搜索,但似乎没有任何东西符合我的确切问题。除非您的文件要更改,否则您可以在运行应用程序时预先阅读整个文件,并将数据放入视图模型的模型列表中 由于这本质上是一个CSV文件,并且

我无法获得如何在文本文件中搜索然后使用模型视图模型获取所需数据的逻辑。 基本上,我必须做一个字典应用程序,我在文本文件中有单词、语言和描述。比如:

猫;英语;它是一种四条腿的动物

在这个模型中,我有一个文本框,客户端在其中写入一个单词,还有两个其他框,其中应该显示单词的语言和描述


我就是不知道如何在这个文件中搜索。我试图在线搜索,但似乎没有任何东西符合我的确切问题。

除非您的文件要更改,否则您可以在运行应用程序时预先阅读整个文件,并将数据放入视图模型的模型列表中

由于这本质上是一个CSV文件,并且假设每个条目都是一行,使用分号作为分隔符,我们可以使用.Net CSV解析器将文件处理到模型中:

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();
    }
}
基本型号:

public class DictionaryEntryModel {
    public string Word { get; set; }
    public string Language { get; set; }
    public string Description { get; set; }
}
使用构造函数填充模型的视图模型示例:

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();
    }
}
公共类字典viewmodel{
//这将是VM中基于INotify的属性
公共列表字典入口{get;set;}
公共词典VIEWMODEL(){
DictionaryEntries=新列表();
//使用[;]分隔符创建解析器
var textFieldParser=newtextfieldparser(newstringreader(File.ReadAllText(filePath)))
{
分隔符=新字符串[]{“;”}
};
而(!textFieldParser.EndOfData)
{
var entry=textFieldParser.ReadFields();
添加(新的DictionaryEntryModel()
{
Word=条目[0],
语言=条目[1],
Description=条目[2]
});
}
//别忘了关门!
textFieldParser.Close();
}
}
现在,您可以使用属性
DictionaryEntries
绑定视图,只要您的应用处于打开状态,它就会将您的完整文件保留为
DictionaryEntryModel
列表


希望这有帮助

这里我不是在讨论MVVM部分,而是讨论如何使用不区分大小写的正则表达式,根据搜索词搜索文本文件以获得结果数据

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;
        }
    }
}
stringdictionaryfilename=@“C:\Test\SampleDictionary.txt”;//替换为您的文件路径
字符串searchedTerm=“Cat”//替换为用户输入字
string searchRegex=string.Format(“^(?{0}”);(?[^;]*);(?*)$”,searchedTerm);
字符串术语;
字符串语言;
字符串描述;
使用(var s=newstreamreader(dictionaryFileName,Encoding.UTF8))
{
弦线;
而((line=s.ReadLine())!=null)
{
var matches=Regex.Match(line、searchRegex、RegexOptions.IgnoreCase);
如果(匹配。成功)
{
foundTerm=matches.Groups[“Term”].Value;
foundLanguage=matches.Groups[“Lang”].Value;
foundDescription=匹配.Groups[“Desc”].Value;
打破
}
}
}
然后可以向用户显示结果字符串


请注意,这将适用于典型的输入字,但如果用户输入干扰正则表达式语法的特殊字符,则可能会产生奇怪的结果。大部分问题可以通过使用
Regex.Escape(searchedTerm)
来纠正,非常感谢。我明白了,真的谢谢@TatianaGancheva我很高兴它对你有用!:)如果你觉得它已经解决了你的问题,请考虑把这个作为答案。