Please bear with me as I am very new to programming itself and C# winforms.
I have an AAA.txt file where I make it appears in a combobox as “AAA”. My main intention is to allow user to choose AAA from the dropdown combo, then click search. On the click event, the function should read the text file’s content line by line and then find if these words (eg hello) or phrases (eg good morning) appear in all my 20 XML files’ <description></description> child nodes. If these words/phrases do appear in certain <description></description> child nodes, then the data of whole <item></item> parent nodes will appear as results.
AAA.txt:
hello
good morning
great
bye
My function:
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");
}
}
}
}
I understand some may tell me to use LINQ-to-XML, but this is not my concern at this point. I know this line if (itemDescription.ToLower().Contains(comboTemplates.SelectedItem.ToString())) doesn’t do what I want (it will search the word “AAA” instead of looking into the selected AAA text file). May I know how can I write this line correctly in order to read the words/phrases appear in the selected text file?
Thank you.
The static
System.IO.Fileclass has a methodReadAllLinesthat reads all the lines of a text file into an array.If the combo contains only the filename, you might want to supplement it with the directory name first
Then put the words into a
HashSet<string>Then split your description into single words using a regular expression
You can do the comparison in many different ways. E.g. by using LINQ
Note: It is not enough to look if a string contains a word with
since it would find parts of words like “greatness” as well.
If you need to find phrases as well, the approach described above does not work. You will need to combine a Regex search with a loop or a LINQ statement. Let’s use a regex expression of the type
\bmatches word boundaries. In order to be sure not to introduce some special regex character into the regex expression we need to escape our word or phrase.Finally we must do this test for all the word and phrases in the list. Let’s put everything together:
Then test your descriptions