2009-12-24 75 views
0

我想根據查詢字符串過濾XML過濾器基於XML的查詢字符串

我的XML是這樣

<Categories> 
    <Category>  
     <Title>Food1<Title> 
     <Cat>Cat3</Cat> 
     <Duration>12/1/2009-12/1/2011</Duration> 
     <Description>Who is hungry</Description> 
    <Category> 
    <Category>  
     <Title>Food1<Title> 
     <Cat>Cat2</Cat> 
     <Duration>12/1/2009-12/1/2011</Duration> 
     <Description>Who is hungry</Description> 
    <Category> 
    <Category>  
     <Title>Food1<Title> 
     <Cat>Cat1</Cat> 
     <Duration>12/1/2009-12/1/2011</Duration> 
     <Description>Who is hungry</Description> 
    <Category> 
    <Category>  
     <Title>Food1<Title> 
     <Cat>Cat1</Cat> 
     <Duration>12/1/2009-12/1/2011</Duration> 
     <Description>Who is </Description> 
    <Category> 

我想基於內容的過濾查詢字符串如 if

  1. ?食物=食物1(它應該顯示所有)
  2. ?食品=食物1 &貓= CAT1(應該dispalay去年2)
  3. ?貓= CAT1 &說明=誰是(顯示last1)
  4. ?食品=食物1 &貓= CAT1 &說明=誰是餓了(第三之一)

我想在repeater.I顯示此我使用XPathIterator遍歷它,並將其綁定到中繼器。

我的代碼到目前爲止是這樣的

string _Cat = HttpContext.Current.Request.QueryString["Cat"].ToString(); 
    string _description = HttpContext.Current.Request.QueryString["description"].ToString(); 
      string _food = HttpContext.Current.Request.QueryString["food"].ToString(); 


      XmlDocument doc = new XmlDocument(); 

      doc.Load("/Finder.xml"); 

      XPathNavigator nav = doc.CreateNavigator(); 



XPathExpression expression = nav.Compile("/Categories/Category[Cat='_Cat and title='_food' and Description='_description']/*"); 
XPathNodeIterator iterator = nav.Select(expression); 

//但這隻能解決案例4)我知道我可以寫其他三個類似的,但我想這件事情是動態的。就像我們在亞馬遜一樣。過濾器只是根據選擇你讓it.so可能沒有查詢字符串添加,可能是1,可能是2,所以我需要檢查對

,然後即時通訊結合這個迭代器來使用轉發器的數據表

任何想法如何實現這一目標?

回答

0

爲什麼不設置一系列if語句來查看哪些參數在查詢字符串中傳遞,然後根據相應的值生成相應的XPath表達式ÿ?

喜歡的東西:

string xpath = "/Categories/Category"; 
string predicate = ""; 

if (_food != "") { 
    predicate += "title='" + _food + "'"; 
} 
if (_Cat != "") { 
    if (predicate!="") { 
     predicate += " and "; 
    } 
    predicate += "Cat='" + _Cat + "'"; 
} 
if (_Description != "") { 
    if (predicate!="") { 
     predicate += " and "; 
    } 
    predicate += "Description='" + _Description + "'"; 
} 

if (predicate!=""){ 
    xpath += "[" + predicate + "]"; 
} 
XPathExpression expression = nav.Compile(xpath); 
+0

我想補充一點,f您使用此解決方案,你必須處理的查詢字符串,以防止XPath注入傳遞的值。 – 2009-12-24 18:43:41

+0

感謝羅蘭的工作精細.... – 2009-12-24 20:47:54

1

你有沒有使用LINQ考慮,但我不能確定你能避免什麼程度repitition

XDocument xmlDoc = XDocument.Load(@"C:\so.xml"); 

// handle Query String variations here - this works for your first case. 
List<Categories> results = (from cats in xmlDoc.Descendants("Category") 
          where cats.Element("Title").Value.ToLower() == "food1" 
          select new Categories 
          { 
           Title = cats.Element("Title").Value, 
           Cat = cats.Element("Cat").Value, 
           Duration = cats.Element("Duration").Value, 
           Description = cats.Element("Description").Value 
          }).ToList(); 

然後,您可以綁定列表...

的分類

佔位類類型

internal class Categories 
    { 
     public string Title { get; set; } 
     public string Cat { get; set; } 
     public string Duration { get; set; } 
     public string Description { get; set; } 
    } 
相關問題