Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:33:54+00:00 2026-06-09T20:33:54+00:00

Please bear with me as I am very new to programming itself and C#

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:33:55+00:00Added an answer on June 9, 2026 at 8:33 pm

    The static System.IO.File class has a method ReadAllLines that reads all the lines of a text file into an array.

    string[] words = File.ReadAllLines(filepath);
    

    If the combo contains only the filename, you might want to supplement it with the directory name first

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

    Then put the words into a HashSet<string>

    var wordSet = new HashSet<string>(words);
    

    Then split your description into single words using a regular expression

    var descrWords = 
        new HashSet<string>(
            Regex.Matches(itemDescription.ToLower(), @"\w+")
            .Cast<Match>()
            .Select(m => m.Value)
        );
    descrWords.UnionWith(wordSet);
    if (descrWords.Count > 0) {
        // Your description contains at least one of the words
    }
    

    You can do the comparison in many different ways. E.g. by using LINQ

    if (words.Union(
        Regex.Matches(itemDescription.ToLower(), @"\w+")
            .Cast<Match>()
            .Select(m => m.Value)
        ).Any())
    {
        ...
    }
    

    Note: It is not enough to look if a string contains a word with

    s.Contains("great")
    

    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

    \bWordOrPhrase\b
    

    \b matches 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.

    bool found = Regex.IsMatch(description, @"\b" + Regex.Escape(wordOrPhrase) + @"\b");
    

    Finally we must do this test for all the word and phrases in the list. Let’s put everything together:

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

    Then test your descriptions

    string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
    if (words.Any(
        wordOrPhrase =>
        Regex.IsMatch(itemDescription,
                      @"\b" + Regex.Escape(wordOrPhrase) + @"\b",
                      RegexOptions.IgnoreCase)))
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am VERY new to mySQL, please bear with me. I have an array
I am very new to programming and Python so please bear with me. I
I am very new to python so please bear with me. I have a
I'm very new to WPF, so please bear with me. Basically I have defined
Please bear with me as I am very new to functional programming and Haskell.
Very new to C so please bear with me. I have a function that
I am very new to Perl, so please bear with my simple question: Here
Okay, I'm very new to Java so please bear with me. I am using
Please bear with me as I am new to dojo, javascript, and web programming
I am very new to using preg_replace and such so please bear with me.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.