0

在我的应用程序中,我使用的是asp.net、c#、jquery,并且我有如下要求:

我有一个 xml 文件“Mobile.xml”,其内容如下:

<Mobiles>
  <Mobile Id="1">
    <MDetails ModelNo="1" Desc="x phone color red"/>
    <MDetails ModelNo="2" Desc="x phone color green"/>
    <MDetails ModelNo="3" Desc="x phone color blue"/>
    <MDetails ModelNo="4" Desc="x phone color black"/>
    <MDetails ModelNo="5" Desc="x phone color yellow"/>
    <MDetails ModelNo="6" Desc="x phone color pink"/>
    <MDetails ModelNo="7" Desc="x phone color gray"/>
    <MDetails ModelNo="8" Desc="x phone color silver"/>
    <MDetails ModelNo="9" Desc="x phone color orange"/>
  </Mobile>
  <Mobile Id="2">
    <MDetails ModelNo="11" Desc="y phone color red"/>
    <MDetails ModelNo="12" Desc="y phone color green"/>
    <MDetails ModelNo="13" Desc="y phone color blue"/>
    <MDetails ModelNo="14" Desc="y phone color black"/>
    <MDetails ModelNo="15" Desc="y phone color yellow"/>
    <MDetails ModelNo="16" Desc="y phone color pink"/>
    <MDetails ModelNo="17" Desc="y phone color gray"/>
    <MDetails ModelNo="18" Desc="y phone color silver"/>
    <MDetails ModelNo="19" Desc="y phone color orange"/>
  </Mobile>
  <Mobile Id="3">
  ......
  <Mobile>
  ......
</Mobiles>

想获取 与多个关键字相关的所有标签的详细信息,其中关键字将从输入搜索字符串中获取。

就像如果搜索字符串是“x phone color red green”那么它应该返回标签的详细信息:

<MDetails ModelNo="1" Desc="x phone color red"/>
<MDetails ModelNo="2" Desc="x phone color green"/>

如果搜索字符串是:“phone color red green”,那么它应该返回:

<MDetails ModelNo="1" Desc="x phone color red"/>
<MDetails ModelNo="2" Desc="x phone color green"/>
<MDetails ModelNo="11" Desc="y phone color red"/>
<MDetails ModelNo="12" Desc="y phone color green"/>

我尝试了以下代码,但还不够:

public void doSearch(string strSearchContent)
{
    string[] strArrSearchContents;
    strArrSearchContents = strSearchContent.Split(' ');

    string fileName = HttpContext.Current.Server.MapPath(@"~\XMLFiles/Mobile.xml");
    XDocument doc = null;
        if (System.IO.File.Exists(fileName))
        {
            doc = XDocument.Load(fileName);

            IEnumerable<XElement> list1 =
                from elements in doc.Descendants("MDetails")
                where
                    elements.Attribute("Desc").Value.Trim().ToLower().Contains(strArrSearchContents[0]) &&
                    elements.Attribute("Desc").Value.Trim().ToLower().Contains(strArrSearchContents[1]) &&
                    elements.Attribute("Desc").Value.Trim().ToLower().Contains(strArrSearchContents[2]) &&
                    elements.Attribute("Desc").Value.Trim().ToLower().Contains(strArrSearchContents[3]) &&
                    elements.Attribute("Desc").Value.Trim().ToLower().Contains(strArrSearchContents[4])
                select elements;

        }
}

上面的代码根本不是这种搜索的解决方案......但我已经描述了我的要求。

4

1 回答 1

0

I wrote something a bit more generically but I think you can use it. Basically, it is a linq query that says: for each word in the description, make sure it is contained in the list of things to find.

string toFind = "x phone color red green";
string[] toFindStuff = toFind.Split(' ');
List<string> desc = new List<string>{ "x phone color red", "y phone color green", "x phone color blue", "x phone color green"};
List<string> filtered = desc.Where(x => x.Split(' ').All(y => toFindStuff.Contains(y))).ToList();

result:

List<String> (2 items) 
x phone color red 
x phone color green 
于 2013-01-08T15:03:12.693 回答