XPathXSLSample1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:4k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Data;
  8. using System.Xml;
  9. using System.Xml.XPath;
  10. namespace Wrox.ProCSharp.Xml.XPathXSLSample1
  11. {
  12. using System.Xml.Xsl;
  13. /// <summary>
  14. /// Summary description for Form1.
  15. /// </summary>
  16. public class Form1 : System.Windows.Forms.Form
  17. {
  18. private System.Windows.Forms.ListBox listBox1;
  19. private System.Windows.Forms.Button button1;
  20. /// <summary>
  21. /// Required designer variable.
  22. /// </summary>
  23. private System.ComponentModel.Container components=null;
  24. public Form1()
  25. {
  26. //
  27. // Required for Windows Form Designer support
  28. //
  29. InitializeComponent();
  30. //
  31. // TODO: Add any constructor code after InitializeComponent call
  32. //
  33. }
  34. /// <summary>
  35. /// Clean up any resources being used.
  36. /// </summary>
  37. protected override void Dispose( bool disposing )
  38. {
  39. if( disposing )
  40. {
  41. if (components != null) 
  42. {
  43. components.Dispose();
  44. }
  45. }
  46. base.Dispose( disposing );
  47. }
  48. #region Windows Form Designer generated code
  49. /// <summary>
  50. /// Required method for Designer support - do not modify
  51. /// the contents of this method with the code editor.
  52. /// </summary>
  53.     private void InitializeComponent()
  54.     {
  55.       this.listBox1 = new System.Windows.Forms.ListBox();
  56.       this.button1 = new System.Windows.Forms.Button();
  57.       this.SuspendLayout();
  58.       // 
  59.       // listBox1
  60.       // 
  61.       this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  62.         | System.Windows.Forms.AnchorStyles.Right)));
  63.       this.listBox1.Location = new System.Drawing.Point(24, 16);
  64.       this.listBox1.Name = "listBox1";
  65.       this.listBox1.Size = new System.Drawing.Size(232, 225);
  66.       this.listBox1.TabIndex = 0;
  67.       // 
  68.       // button1
  69.       // 
  70.       this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  71.       this.button1.Location = new System.Drawing.Point(40, 256);
  72.       this.button1.Name = "button1";
  73.       this.button1.Size = new System.Drawing.Size(200, 24);
  74.       this.button1.TabIndex = 1;
  75.       this.button1.Text = "Load XML";
  76.       this.button1.Click += new System.EventHandler(this.button1_Click);
  77.       // 
  78.       // Form1
  79.       // 
  80.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  81.       this.ClientSize = new System.Drawing.Size(288, 293);
  82.       this.Controls.Add(this.button1);
  83.       this.Controls.Add(this.listBox1);
  84.       this.Name = "Form1";
  85.       this.Text = "Form1";
  86.       this.ResumeLayout(false);
  87.     }
  88. #endregion
  89. /// <summary>
  90. ///  The main entry point for the application.
  91. /// </summary>
  92. [STAThread]
  93. static void Main()
  94. {
  95. Application.Run(new Form1());
  96. }
  97.     // XPathXSLSample1/form1.cs
  98.     private void button1_Click(object sender, System.EventArgs e)
  99.     {
  100.       //modify to match your path structure
  101.       XPathDocument doc=new XPathDocument("..\..\..\booksxpath.xml");
  102.       //create the XPath navigator
  103.       XPathNavigator nav=((IXPathNavigable)doc).CreateNavigator();
  104.       //create the XPathNodeIterator of book nodes
  105.       // that have genre attribute value of novel
  106.       XPathNodeIterator iter=nav.Select("/bookstore/book[@genre='novel']");
  107.       
  108.       while(iter.MoveNext()) {
  109.         LoadBook(iter.Current);
  110.       }
  111.     }
  112.     
  113.     private void LoadBook(XPathNavigator lstNav)
  114.     {
  115.       //We are passsed an XPathNavigator of a particular book node
  116.       //we will select all fo teh direct descendents and
  117.       //load the list box with the names and values
  118.       XPathNodeIterator iterBook=lstNav.SelectDescendants(XPathNodeType.Element,false);
  119.       while(iterBook.MoveNext())
  120.         listBox1.Items.Add(iterBook.Current.Name + ": " + iterBook.Current.Value);
  121.     }
  122.  
  123. }
  124. }