2013-01-08 324 views
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手機紅色綠色」,那麼它應該返回一個標記的細節:

<MDetails ModelNo="1" Desc="x phone color red"/> 
<MDetails ModelNo="2" Desc="x phone color 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; 

     } 
} 

上述代碼根本不是這種搜索類型的解決方案... 但我描述了我的要求。

+0

我想你可以吐出你的搜索關鍵詞爲兩個parts.and然後開始搜索? – bystander

+2

'我嘗試了不夠的波紋管代碼 - 爲什麼不? – webnoob

回答

0

我寫了一些更一般的東西,但我認爲你可以使用它。基本上,這是一個linq查詢,它說:對於描述中的每個單詞,確保它包含在要查找的事物列表中。

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(); 

結果:

List<String> (2 items) 
x phone color red 
x phone color green 
+0

@ pks如果這對你有用,請點擊支票「接受」我的答案 – Brandon