该文档包含数个<book>节点,每个<book>节点有一个子节点<PublishDate>,如何获取在一个时间段内的<PublishDate>的<book>节点。

解决方案 »

  1.   

    LINQ TO XML 查询

       string xml = @"<root>
              <Field>
                  <!-- Name of Stencil the condition is recorded on. Defined by BCA -->
                  <FieldName>StencilName</FieldName>
                  <FieldValue>Car - Internal - Portrait</FieldValue>
                </Field>
                <Field>
                  <!-- X coordinate of condition item -->
                  <FieldName>X</FieldName>
                  <FieldValue>123</FieldValue>
                </Field>
                <Field>
                  <!-- Y coordinate of condition item -->
                  <FieldName>Y</FieldName>
                  <FieldValue>233</FieldValue>
           </Field>
    </root>";
                XElement root = XElement.Parse(xml);
                var query = root.Descendants("Field").Where(x => new string[] { "X", "Y" }.Contains(x.Element("FieldName").Value)).Select(x => x.Element("FieldValue").Value).ToList();
                foreach (var q in query)
                {
                    Console.WriteLine(q);
                }
      

  2.   

    使用linq,附一个教程:-------------------XML code  <Employees>    <Employee name="李宇秋" age="23">      <Address>         长江路178号      </Address>      <Department>         演唱部      </Department>    </Employee>    <Employee name="曾不可" age="24">      <Address>         火星路239号     </Address>      <Department>         舞蹈部     </Department>    </Employee>    <Employee name="张学敌" age="27">      <Address>         香港街13号     </Address>      <Department>         偶像部     </Department>    </Employee>  </Employees>     --------------比如:要获取 name="曾不可" 的节点下面的 Address 节点的值“火星路239号”,C#如何实现? OK,在linq之前,可以试用XMLDocment将xml整个文件读进来,然后比如可以用xpath再进行分析。
    自从有了linq,一切都变得简单了,实现代码如下:view sourceprint?//假设以上xml我们保存在一个本地xml文件 D:\Microsoft Work\XMLPath\sampleXML.xml 中  public List<string> GetXMLResult()  {         XElement xelement = XElement.Parse(@"D:\Microsoft Work\XMLPath\sampleXML.xml");         var query = xelement.Descendants("Employee").Where(x => x.Attribute("name").Value == "曾不可").Select(x => x.Element("Address").Value);         if (query != null && query.Count() > 0)               return query.ToList<string>();            return null;  }  这样即可。 OK,我们现在将上面的XML结构稍作改动一下,view sourceprint?-------------------XML code  <Employees>    <Employee name="李宇秋" age="23">      <Address>         <Province>            长江省         </Province>         <Road>            长江路178号       </Road>     </Address>      <Department>         演唱部      </Department>    </Employee>    <Employee name="曾不可" age="24">       <Address>         <Province>            火星省       </Province>         <Road>            火星路239号       </Road>      <Address>      <Department>         舞蹈部     </Department>    </Employee>    <Employee name="张学敌" age="27">      <Address>         <Province>            香港省       </Province>         <Road>            香港街239号       </Road>      <Address>      <Department>         偶像部     </Department>    </Employee>  </Employees>     --------------现在:仍然是要获取 name="曾不可" 的节点下面的 Address 节点下Road结点的值“火星路239号”, 
    view sourceprint?--------------C#如何实现?  OK,这时候linq的优势就很明显了,实现代码如下:view sourceprint?private List<string> GetXMLResult(string xmlPath, string employeeName)  {       XElement xelement = XElement.Load(xmlPath);       var query = xelement.Descendants("Employee").SingleOrDefault(x => x.Attribute("name").Value == employeeName);       if (query != null && query.Count() > 0)       {           var queryResult = query.Descendants("Road").Select(m => m.Value);           if (queryResult.Count() > 0)              return queryResult.ToList<string>();       }       return null;  } 如果没有Linq To XML,用xpath也可以解决这样的取值问题,但是会变得复杂很多。
      

  3.   


    又没看清楼主的问题,晕死呵呵你要搜索的是不是publishdate>2011-1-2 and publishdate<2011-3-5之类的节点呢?
    还是用LINQ,把我和如梦大大回复的LINQ语句简单修改一下就行了
      

  4.   

    毛线,我反而觉得LINQ很麻烦,效率又还相当低下!
      

  5.   

    用linq,先找到那个节点,然后用from ...in... where判断节点值的时间... select...