2009-12-24 72 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; } 
    }