XPathXSLSample2.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. using System.Xml.Xsl;
  11. namespace Wrox.ProCSharp.Xml.XPathXSLSample2
  12. {
  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.     // XPathXSLSample2/form1.cs
  98.     private void button1_Click(object sender, System.EventArgs e) {
  99.       //modify to match your path structure
  100.       XPathDocument doc=new XPathDocument("..\..\..\booksxpath.xml");
  101.       //create the XPath navigator
  102.       XPathNavigator nav=((IXPathNavigable)doc).CreateNavigator();
  103.       //create the XPathNodeIterator of book nodes
  104.       // that have genre attribute value of novel
  105.       XPathNodeIterator iter=nav.Select("/bookstore/book[@genre='novel']");
  106.       
  107.       while(iter.MoveNext()) {
  108.         LoadBook(iter.Current.Clone());
  109.       }
  110.       //add a break line and calculate the sum
  111.       listBox1.Items.Add("========================");
  112.       listBox1.Items.Add("Total Cost = " + nav.Evaluate("sum(/bookstore/book[@genre='novel']/price)"));
  113.     }
  114.     
  115.     private void LoadBook(XPathNavigator lstNav) {
  116.       //We are passsed an XPathNavigator of a particular book node
  117.       //we will select all of the direct descendents and
  118.       //load the list box with the names and values
  119.       XPathNodeIterator iterBook=lstNav.SelectDescendants(XPathNodeType.Element,false);
  120.       while(iterBook.MoveNext())
  121.         listBox1.Items.Add(iterBook.Current.Name + ": " + iterBook.Current.Value);
  122.     }
  123.  
  124. }
  125. }