XML学习笔记(七):XPath数据模式

  • XPath数据模式依赖存System.Xml.XPath命名空间的XPathNavigator类。
  • XPathNavigator类是一个抽象类,是一个以指针方式为基础的浏览XML数据的模式,它允许你对XML文件进行编辑。
  • 你可以从任何实现了IXPathNavigable接口的类获得XPathNavigator类的一个实例。XmlDocument和XPathDocument就是实现了这个接口的类。
  • XmlDocument返回的XML是可编辑的,XPathDocument返回对象是只读的。

创建XPathNavigator对象:

 

   1:    //功能性示例代码:
   2:  private void button1_Click(object sender, EventArgs e)
   3:      {
   4:          XPathNavigator navigator = null;
   5:          if (radioButton1.Checked)
   6:          {
   7:              //XmlDocument类创建XPathNavigator对象
   8:              XmlDocument doc = new XmlDocument();
   9:              doc.Load(Application.StartupPath + @"/employees.xml");
  10:              navigator = doc.CreateNavigator();
  11:          }
  12:          else
  13:          {
  14:              //XmlPathDocument类创建XPathNavigator对象
  15:              XPathDocument doc = new XPathDocument(Application.StartupPath + @"/employees.xml");
  16:              navigator = doc.CreateNavigator();
  17:          }
  18:          MessageBox.Show("Navigator created successfully!");
  19:      }

通过XPathNavigator来浏览XML文档

 

   1:   protected void Button1_Click(object sender, EventArgs e)
   2:      {
   3:          XPathDocument doc =new XPathDocument(xmlFilePath);
   4:          XPathNavigator navigator = doc.CreateNavigator();
   5:          navigator.MoveToRoot();
   6:          navigator.MoveToFirstChild();        
   7:          TreeNode root = TreeView1.Nodes.Add("Employees");
   8:          while (navigator.MoveToNext())
   9:          {
  10:              if (navigator.HasChildren)
  11:              {
  12:                  navigator.MoveToFirstChild();
  13:                  do
  14:                  {
  15:                      string id = navigator.GetAttribute("employeeid", "");
  16:                      TreeNode empnode = new TreeNode("Employee ID :" + id);
  17:                      root.Nodes.Add(empnode);
  18:                      navigator.MoveToFirstChild();
  19:                      do
  20:                      {
  21:                          string name = navigator.Name;
  22:                          TreeNode node = new TreeNode(name + " : " + navigator.Value);
  23:                          empnode.Nodes.Add(node);
  24:                      } while (navigator.MoveToNext());
  25:                      navigator.MoveToParent();
  26:                  }
  27:                  while (navigator.MoveToNext());
  28:              }
  29:          }
  30:      }
 
参考书籍
 
XSLT 2.0 and XPath 2.0 Programmer's Reference (Programmer to Programmer)
31bJBBvsybL._SL160_.jpg

转载于:https://www.cnblogs.com/apiaceae/archive/2009/05/10/1453481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值