【问题标题】:Parsing Through XML Files with Selected Keywords使用选定的关键字解析 XML 文件
【发布时间】:2012-08-08 19:15:11
【问题描述】:

我有以下代码。假设用户可以插入1-4个关键字并点击搜索按钮,如果子标签<description</description>中的内容包含一个/多个输入的关键字,那么在richtextbox中会出现显示整个<item></item>的结果。但是代码在if (itemDescription.Contains(txtComKeyword1 | txtComKeyword2 | txtComKeyword3 | txtComKeyword4)这一行不正确。

请大家看看好吗?非常感谢您的帮助!谢谢。

以下是我的 XML 文件结构的一部分:

<item>
      <title>[PhoTosynthesIs] Being driven</title>
      <author>PhoTosynthesIs</author>
      <description>purely by profit, I've decided to stick to my strategy and exit both tranches at 177. Will pick this stock up again when it breaches and holds the next pivot point. gl all</description>
      <link>http://www.lse.co.uk/shareChat.asp?ShareTicker=BARC&amp;post=5660817</link>
      <pubDate>Wed, 08 Aug 2012 11:43:17 GMT</pubDate>
</item>
<item>
      <title>[b36m] alw51</title>
      <author>b36m</author>
      <description>Could you share your thoughts/opinions  on a buy in price based on TW with me please   many thanks</description>
      <link>http://www.lse.co.uk/shareChat.asp?ShareTicker=BARC&amp;post=5660636</link>
      <pubDate>Wed, 08 Aug 2012 11:16:56 GMT</pubDate>
</item>

下面是我的这个函数的一段代码:

private void searchComByKeywords()
{
    // 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 (itemDescription.Contains(txtComKeyword1 | txtComKeyword2 | txtComKeyword3 | txtComKeyword4)
                {
                    string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                    string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                    string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;

                    richComResults.AppendText("Author: " + 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);
        }
    }
}

【问题讨论】:

    标签: c# xml winforms keyword


    【解决方案1】:

    也许你可以试试:

    if (itemDescription.Contains(txtComKeyword1) || itemDescription.Contains(txtComKeyword2) || itemDescription.Contains(txtComKeyword3) || itemDescription.Contains(txtComKeyword4))
    {
    ...
    }
    

    【讨论】:

    • 我这样做了:if (txtComKeyword1.Text != (String.Empty) &amp;&amp; itemDescription.ToLower().Contains(txtComKeyword1.Text.ToLower()) || txtComKeyword2.Text != (String.Empty) &amp;&amp; itemDescription.ToLower().Contains(txtComKeyword2.Text.ToString()) || txtComKeyword3.Text != (String.Empty) &amp;&amp; itemDescription.ToLower().Contains(txtComKeyword3.Text.ToString()) || txtComKeyword4.Text != (String.Empty) &amp;&amp; itemDescription.ToLower().Contains(txtComKeyword4.Text.ToString())) 并且有效。 :) 但是代码好像很长。
    【解决方案2】:

    if 语句中的条件很复杂,其含义不是很清楚时,我倾向于将其重构为一个单独的方法。在您的情况下,它可能如下所示:

    private static bool DoesItemDescriptionContainOneOf(string description, params string[] items)
    {
        return items.Any(description.Contains);
    }
    

    那么,你的情况会是这样的:

    if (DoesItemDescriptionContainOneOf(itemDescription, txtComKeyword1, txtComKeyword2, txtComKeyword3, txtComKeyword4))
    {
        ....
    }
    

    整洁多了,嗯?

    【讨论】:

    • 它看起来很整洁,但我真的不明白如何应用它,因为我在 Any 和整个 if 语句下有一条红线。自 2 周前以来,我对 C# 非常陌生。 :|
    • @Shyuan Any 是一个位于命名空间System.Linq 中的扩展方法。将此命名空间的 using 指令(即using System.Linq;)添加到文件顶部,它将起作用。我建议阅读有关扩展方法和 LINQ 的更多信息 - 通常,它们会真正简化您的代码。
    • 您好 Nikola,Any 问题已修复,但 if 语句在 DoesItemDescriptionContainOneOf(itemDescription, txtComKeyword1, txtComKeyword2, txtComKeyword3, txtComKeyword4) 下仍显示红线。你知道发生了什么吗?该错误表示它有一些无效的参数。谢谢。
    • @Shyuan txtComKeyword 参数必须是string 类型。从您的评论到 tanathos 的回答,我可以看出它们可能是文本框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    相关资源
    最近更新 更多