xml相关 --xpath介绍

越来越多的地方用到了xml,如何便捷的操作xml是每个程序员需要掌握的技术,XPath就是用来查询xml文档内容的语言。

一个简单的例子
<?xml version="1.0" encoding="utf-8" ?>
<books>
  
<book>
    
<title>A beginners guide to XPath</title>
    
<author>Gary Francis</author>
    
<description>A book that explains XPath for beginners</description>
    
<data type="Price">12.00</data>
    
<data type="ISBN">1234567890</data>
  
</book>
  
<book>
    
<title>Understanding C# for beginners</title>
    
<author>Any body</author>
    
<description>How to get started with C# and .NET</description>
    
<data type="Comment">Excellent material if you new to C#.</data>
  
</book>
</books>

加载xml
XmlDocument document = null;
XmlNodeList nodeList 
= null;
XmlNode node 
= null;
// Try and load xml data into an Xml document object and throw an
// error message if this fails
try
{
    document 
= new XmlDocument();
    document.Load(
"Data.xml");
}
catch (Exception ex)
{
    MessageBox.Show(
"Error loading 'Data.xml'. Exception: " + ex.Message);           
}

显示每本书的title信息
// Try and retrieve all book nodes
nodeList = document.SelectNodes("/books/book");

foreach (XmlNode book in nodeList)
{
    
// Show a message with the book title
    MessageBox.Show(book.SelectSingleNode("title").InnerText);
}

XPath Tutorial 以非常直观的图形界面,描述了XPath是如何获取xml中各种特定的内容。
XPath Syntax 详细介绍了XPath的相关语法。

使用xml的Helper类会更加的便捷,相关文章如下:
XML for beginners and experts
Accessing XML Using Insert/Update/Delete/Query Statements

posted @ 2007-05-28 11:11 jecray 阅读(170) 评论(0) 编辑 收藏