高分求解:用.NET中查询XML文档时,支不支持多条件查询,以及支不支持变量
我有如下两个代码段,
1.一个XML文档,booksort.xml它将是我们查询的目标:
<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>2395</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>ood</last-name>
</author>
<price>1335</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
另一个是代码段,运行该代码段,可以达到修改XML文档中符合条件的节点
sample.cs
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");
XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;
nodeList=root.SelectNodes("descendant::book[author/first-name='Margaret']");
//Change the price on the books.
foreach (XmlNode book in nodeList)
{
book.LastChild.InnerText="15.95";
}
Console.WriteLine("Display the modified XML document....");
doc.Save(Console.Out);
}
}
但是在这个代码sample.cs中nodeList=root.SelectNodes("descendant::book[author/first-name='Margaret']");只有一个查询条件,我希望查询到的结点是不仅符合descendant::book[author/first-name='Margaret']"还要符合descendant::book[author/last-name='Atwood']". 这里我就不知道.NET是否支持多条件查询了,如果支持该如何写。
这里是以节点的多查询为基础,那么它又支不支持多属性的查询呢?
还有在它支不支持变量呢?比如在这个程序中first-name='Margaret',如果first-name会根据需求变化,而给descendant::book[author/first-name='变量']"传送一个变量。这能行吗?
如果这以上的三个功能都无法用这种查询方法实现的话,又该如何去实现它。
各位高手!这个问题似乎有难度!只有达到这样的目的,XML才能与数据库相抗衡,否则就无法查到满意的结果。