结合4种功能的困惑

时间:2012-08-20 03:51:52

标签: c# xml winforms

如果我的代码杂乱或笨重,请耐心等待我,我是一个真正的初学者,一个月前开始编程,根本没有背景。我现在创建了4个函数searchComByAuthor();searchComByStartDate();searchComByEndDate();searchComByKeywords();。但问题是,我不知道如何将它们组合起来并使它们像过滤器一样。用户可以选择填写任何文本框,当用户点击"分析"按钮,组合功能将完成工作。现在他们都是分开工作,我只是逐个调用这些函数来测试它们是否有效。

截图:

Filters

searchComByAuthor

private void searchComByAuthor()
{
    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        try
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

            string docPath = fileName;

            xmlDoc.Load(docPath); //* load the XML document from the specified file.

            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

            foreach (XmlNode node in nodeList)
            {

                XmlElement itemElement = (XmlElement)node;

                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;

                if (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLower())
                {
                    string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                    string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                    string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
                    string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                    richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
                                                "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");

                }
                //else
                //{
                //    richComResults.AppendText("There is no author " + txtComAuthor.Text.ToString().ToLower() + ". Please ensure you are using a correct author name.");
                //}
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    int pointer = 0;
    int index = 0;
    string keyword = txtComAuthor.Text;
    string shadow = richComByTemplate.Text.ToLower();

    while (true)
    {
        //Searching in the copy/shadow
        index = shadow.IndexOf(keyword, pointer);
        //if keyword not found then the loop will break
        if ((index == -1) || (String.IsNullOrEmpty(keyword)))
        {
            break;
        }
        richComByTemplate.Select(index, keyword.Length);
        richComByTemplate.SelectionColor = Color.Red;
        richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
        pointer = index + keyword.Length;
    }
}

searchComByStartDate

private void searchComByStartDate()
{

    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

        string docPath = fileName;

        xmlDoc.Load(docPath); //* load the XML document from the specified file.

        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

        foreach (XmlNode node in nodeList)
        {

            XmlElement itemElement = (XmlElement)node;

            string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
            CultureInfo provider = CultureInfo.InvariantCulture;
            if (DateTime.Parse(itemDate) >= DateTime.ParseExact(txtComStartDate.Text, "dd/MM/yy", provider))
            {
                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
                string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
                string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate + 
                                            "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
            }
        }
    }
}

searchComByEndDate

private void searchComByEndDate()
{
    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

        string docPath = fileName;

        xmlDoc.Load(docPath); //* load the XML document from the specified file.

        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

        foreach (XmlNode node in nodeList)
        {

            XmlElement itemElement = (XmlElement)node;

            string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
            CultureInfo provider = CultureInfo.InvariantCulture;
            if (DateTime.Parse(itemDate) <= DateTime.ParseExact(txtComEndDate.Text, "dd/MM/yy", provider))
            {
                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
                string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
                string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate + 
                                            "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
            }
        }
    }
}

searchComByKeywords

private void searchComByKeywords()
{
    List<TextBox> boxes = new List<TextBox>();
    boxes.Add(txtComKeyword1);
    boxes.Add(txtComKeyword2);
    boxes.Add(txtComKeyword3);
    boxes.Add(txtComKeyword4);

    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        try
        {
            XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

            string docPath = fileName;

            xmlDoc.Load(docPath); //* load the XML document from the specified file.

            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

            foreach (XmlNode node in nodeList)
            {
                XmlElement itemElement = (XmlElement)node;

                string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;

                if (txtComKeyword1.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword1.Text.ToLower()) ||
                    txtComKeyword2.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword2.Text.ToLower()) ||
                    txtComKeyword3.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword3.Text.ToLower()) ||
                    txtComKeyword4.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword4.Text.ToLower()))
                {
                    string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                    string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                    string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
                    string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);

                    richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate + 
                                                "\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");

                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    foreach (TextBox box in boxes)
    {
        int pointer = 0;
        int index = 0;
        string keyword = box.Text;
        string shadow = richComByTemplate.Text.ToLower();

        while (true)
        {
            //Searching in the copy/shadow
            index = shadow.IndexOf(keyword, pointer);
            //if keyword not found then the loop will break
            if ((index == -1) || (String.IsNullOrEmpty(keyword)))
            {
                break;
            }
            //Customising the original data
            richComByTemplate.Select(index, keyword.Length);
            richComByTemplate.SelectionColor = Color.Red;
            richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
            pointer = index + keyword.Length;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的功能中有大量重复的代码。

通过将四个功能组合到一个功能中,您可以在简化代码的同时实现目标。为该单个函数提供一个参数,该参数是为了包含给定记录必须满足的条件列表(假设所有条件必须适用于选择记录)。

在您的情况下,您希望直接匹配在一个或多个文本字段中输入的内容。将这些文本字段的值传递给新的组合方法(以便您可以将逻辑与UI分开),并且对于具有非null,非空值的每个值,应用适当的逻辑测试。如果传入Author和StartDate,则应用与Author和StartDate相关的逻辑。如果两个测试都通过,请包含该记录。

为了“包含该记录”,而不是执行代码当前正在做的事情:

richComByTemplate.AppendText(...)

您可能希望返回结果列表并让调用者执行附加文本(再次,将您的用户界面与搜索逻辑分开)。