-- 作者:dakzhang
-- 发布时间:5/25/2006 12:26:00 PM
-- NET中如何判断XPATH语句执行结果为空!!!
废了九牛二虎之力,终于找到了 使用Object.ReferenceEquals 方法,具体代码如下: XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("books.xml")); XmlElement root; root = (XmlElement)doc.SelectSingleNode("/booksort/book[1]/prices"); bool Is_Null=object.ReferenceEquals(root,null); if(Is_Null == true) { Response.Write("Xpath表达式执行结果为空"); } books.xmll如下: <?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>29.95</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>
|