根据查询字符串过滤xml

时间:2009-12-24 18:11:54

标签: c# xml xpath repeater

我想根据查询字符串

过滤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>

我希望根据查询字符串进行过滤 如果

  1. ?food = Food1(应显示全部)
  2. ?food = Food1&amp; Cat = Cat1(它应该显示过去2)
  3. ?Cat = Cat1&amp; Description = Who is(显示last1)
  4. ?food = Food1&amp; Cat = Cat1&amp; Description =谁饿了(第3名)
  5. 我想在转发器中显示它。我正在使用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)我知道我可以为其他三个写相似但我希望这个东西是动态的。就像我们在亚马逊一样。过滤只是根据你的选择添加它。所以可能没有查询字符串,可能是1,可能是2所以我需要检查那个

    然后我使用数据表

    将此迭代器绑定到转发器

    知道如何实现这个目标吗?

2 个答案:

答案 0 :(得分:1)

您是否考虑过使用linq,虽然我不确定您可以避免重复的程度

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; }
    }

答案 1 :(得分: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);