Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 读取文本文件中的行,然后查看XML文件是否包含文本文件中出现的单词_C#_Winforms - Fatal编程技术网

C# 读取文本文件中的行,然后查看XML文件是否包含文本文件中出现的单词

C# 读取文本文件中的行,然后查看XML文件是否包含文本文件中出现的单词,c#,winforms,C#,Winforms,请容忍我,因为我对编程本身和C#winforms非常陌生 我有一个AAA.txt文件,我将它显示在一个组合框中作为“AAA”。我的主要目的是允许用户从下拉组合中选择AAA,然后单击搜索。在click事件中,函数应该逐行读取文本文件的内容,然后查找这些单词(如hello)或短语(如good morning)是否出现在my 20 XML文件的所有子节点中。如果这些单词/短语确实出现在某些子节点中,则整个父节点的数据将显示为结果 AAA.txt: hello good morning great by

请容忍我,因为我对编程本身和C#winforms非常陌生

我有一个
AAA.txt
文件,我将它显示在一个组合框中作为“AAA”。我的主要目的是允许用户从下拉组合中选择AAA,然后单击搜索。在click事件中,函数应该逐行读取文本文件的内容,然后查找这些单词(如hello)或短语(如good morning)是否出现在my 20 XML文件的所有子节点中。如果这些单词/短语确实出现在某些
子节点中,则整个
父节点的数据将显示为结果

AAA.txt:

hello
good morning
great
bye
我的职能:

private void searchComByKeywords()
{ 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        XmlDocument xmlDoc = new XmlDocument();
        string docPath = fileName;
        xmlDoc.Load(docPath);
        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

        foreach (XmlNode node in nodeList)
        {
            XmlElement itemElement = (XmlElement)node;
            string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;

            if (itemDescription.ToLower().Contains(comboTemplates.SelectedItem.ToString()))
            {
                string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;

                richComByTemplate.AppendText("Author: " + itemAuthor + "\nDate: " + itemDate + "\nTitle: " + itemTitle + "\nDescription: " + itemDescription + "\n\n--------\n\n");
            }
        }
    }
}
我知道有些人可能会告诉我使用LINQ到XML,但这不是我现在关心的问题。我知道这一行
如果(itemsdescription.ToLower().Contains(comboTemplates.SelectedItem.ToString())
不符合我的要求(它将搜索单词“AAA”,而不是搜索所选的AAA文本文件)。我可以知道如何正确地写这一行以阅读所选文本文件中出现的单词/短语吗


谢谢。

静态
System.IO.File
类有一个方法
ReadAllLines
,它将文本文件的所有行读取到一个数组中

string[] words = File.ReadAllLines(filepath);
如果组合框只包含文件名,您可能希望首先用目录名对其进行补充

string dir = @"C:\MyDataPath";
string filename = comboTemplates.SelectedItem.ToString();
string filepath = Path.Combine(dir, filename);
然后将单词放入一个
哈希集

因为它也会找到像“伟大”这样的词


如果您还需要查找短语,则上述方法不起作用。您需要将正则表达式搜索与循环或LINQ语句相结合。让我们使用以下类型的正则表达式

\bWordOrPhrase\b
\b
匹配单词边界。为了确保不在正则表达式中引入一些特殊的正则字符,我们需要对单词或短语进行转义

bool found = Regex.IsMatch(description, @"\b" + Regex.Escape(wordOrPhrase) + @"\b");
最后,我们必须对列表中的所有单词和短语进行测试。让我们把一切放在一起:

string dir = @"C:\MyDataPath";
string filename = comboTemplates.SelectedItem.ToString();
string filepath = Path.Combine(dir, filename);

string[] words = File.ReadAllLines(filepath);
然后测试你的描述

string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
if (words.Any(
    wordOrPhrase =>
    Regex.IsMatch(itemDescription,
                  @"\b" + Regex.Escape(wordOrPhrase) + @"\b",
                  RegexOptions.IgnoreCase)))
{
    ...
}

您知道问题所在-替换传递给.Contains()的数据。您要搜索的数据来自哪里?(即“你好”)。硬编码?其他文件?其他控件?单词“你好”或短语“早上好”将来自文本文件。我不知道如何逐行阅读特定文本文件中的这些单词/短语,然后搜索这些单词是否出现在XML文件中?我想不出我应该在“如果”陈述中写些什么。我被困在那里了,先生,谢谢。但是我可以知道如何编写像这样的
if
语句
if(itemsdescription.ToLower().Contains(wordSet))
?我不太确定如何写,因为它会返回错误。如何表示“如果子节点包含数组中的单词/短语”?谢谢。我有点迷路了。我可以知道在
if(descrWords.Count>0){}
中应该做什么吗?困难在于您有两组单词(或短语)。文件中的文字和描述中的文字。很容易看出一个单词是否包含在字符串中;然而,测试列表中的任何单词是否包含在文本中并不容易。我希望我草拟的第二个解决方案能够解决您的问题。我真的不知道我是否应该把这一行
boolfund=Regex.IsMatch(description,@“\b”+Regex.Escape(wordOrPhrase)+@“\b”)
at?
string.Contains(x)
仅当字符串
x
包含在另一个字符串中时才进行查看。它不适用于整个列表。
\bWordOrPhrase\b
bool found = Regex.IsMatch(description, @"\b" + Regex.Escape(wordOrPhrase) + @"\b");
string dir = @"C:\MyDataPath";
string filename = comboTemplates.SelectedItem.ToString();
string filepath = Path.Combine(dir, filename);

string[] words = File.ReadAllLines(filepath);
string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
if (words.Any(
    wordOrPhrase =>
    Regex.IsMatch(itemDescription,
                  @"\b" + Regex.Escape(wordOrPhrase) + @"\b",
                  RegexOptions.IgnoreCase)))
{
    ...
}