×

xpath语法 at

如何用XPath表达式查询XML?repeating,revising的异同

admin admin 发表于2022-07-02 06:04:32 浏览113 评论0

抢沙发发表评论

如何用XPath表达式查询XML


XPath 用于以编程方式计算表达式并选择文档中的特定节点。 要求 下面的列表列出了推荐使用的硬件、软件、网络基础结构以及所需的服务包: �6�1 Visual C# .NET 本文假定您熟悉下列主题: �6�1 XML 术语 �6�1 创建和读取 XML 文件 �6�1 XPath 语法 返回页首 如何用 XPath 表达式查询 XML 1. 在 Visual Studio .NET 中新建一个 Visual C# .NET 控制台应用程序。 备注 : 本示例使用名为 Books.xml 的文件。您可以创建自己的 Books.xml 文件,也可以 使用 .NET 软件开发工具包 (SDK) 快速入门中包括的示例。如果您没有安装 快速入门 而且也不想安装它们,请参阅 Books.xml 下载位置的 参考 部分。如果已经安装了 快速入门 ,则该文件位于以下文件夹中: Program Files\Microsoft.NET\FrameworkSDK\Samples\Quickstart\Howto\Samples\Xml\Transformx ml\VB 必须将该文件复制到 \Bin\Debug 文件夹,该文件夹位于您在其中创建该项目的文件夹中。 2. 确保该项目引用 System.Xml 名称空间。 3. 在 Xml 和 XPath 名称空间上使用 using 语句,这样以后就不需要在代码中限定这些名称空间中的声明了。using 语句必须在所有其他声明之前使用,如下所示: using System.Xml; using System.Xml.XPath; 4. 声明合适的变量。声明 XPathDocument 对象以保存 XML 文档,声明 XpathNavigator 对象以计算 XPath 表达式,声明 XPathNodeIterator 对象以迭代通过选定节点。声明 String 对象以保存 XPath 表达式。在 Class1 的 Main 函数中添加声明代码。 XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; String strExpression; 5. 用示例文件 Books.xml 加载 XPathDocument 。XPathDocument 类使用可扩展样式表语言转换 (XSLT) 为 XML 文档处理提供快速和面向性能的缓存。它类似于 XML 文档对象模型 (DOM),但经过了高度优化,以用于 XSLT 处理和 XPath 数据模型。 // Open the XML. docNav = new XPathDocument(@ c:\books.xml ); 6. 从文档创建 XPathNavigator 。XPathNavigator 对象用于进行只读 XPath 查询。XPath 查询可返回结果值或许多节点。 // Create a navigator to query with XPath. nav = docNav.CreateNavigator(); 7. 创建 XPath 表达式以查找图书的平均价格。这个 XPath 表达式返回单个值。有关 XPath 语法的完整详细信息,请参见 参考 部分中的 XPath 语法 。 // Find the average cost of a book. // This expression uses standard XPath syntax. strExpression = sum(/bookstore/book/price) div count(/bookstore/book/price) ; 8. 使用 XPathNavigator 对象的 Evaluate 方法计算 XPath 表达式。Evaluate 方法返回该表达式的结果。 // Use the Evaluate method to return the evaluated expression. Console.WriteLine( The average cost of the books are {0} , nav.Evaluate(strExpression)); 9. 创建 XPath 表达式以查找价格超过 10 美元的所有图书。这个 XPath 表达式只从 XML 源中返回 Title 节点。 // Find the title of the books that are greater then $10.00. strExpression = /bookstore/book/title[../price 10.00] ; 10. 为使用 XPathNavigator 的 Select 方法选择的节点创建 XPathNodeIterator 。 XPathNodeIterator 表示 XPath 节点集,因此它支持针对该节点集执行的操作。 // Select the node and place the results in an iterator. NodeIter = nav.Select(strExpression); 11. 使用从 XPathNavigator 的 Select 方法返回的 XPathNodeIterator 遍历选定的节点。在这种情况下,可使用 XPathNodeIterator 的 MoveNext 方法迭代通过选定的所有节点。 Console.WriteLine( List of expensive books: ); //Iterate through the results showing the element value. while (NodeIter.MoveNext()) { Console.WriteLine( Book Title:{0} , NodeIter.Current.Value); }; 12. 使用 ReadLine 方法在控制台显示的末尾添加 pause,以便更容易地显示上述结果。 //Pause Console.ReadLine(); 13. 生成并运行您的项目。请注意,这些结果显示在控制台窗口中。

repeating,revising的异同


两个词有很大区别。
repeat:
重复(说明/讲述) to say or tell people something more than once
重复(发生/做某件事) to happen, or to do something, more than once

revise:
改变(主意)、修改(作文) to look at or consider again an idea, piece of writing, etc. in order to correct or improve it
(为了考试)复习 to study again something you have already learned, in preparation for an exam

如果有哪里解释的不清楚,欢迎继续追问。

vb中validate是什么意思


Validate事件

在焦点转换到一个(第二个)控件之前发生,此时该控件的 CausesValidation 属性值设置为True。

语法

Private Sub object_Validate(KeepFocus As Boolean)
-xpath语法