【发布时间】: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&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&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);
}
}
}
【问题讨论】: