DOMSample2.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.Data;
  7. using System.Xml;
  8. namespace Wrox.ProCSharp.Xml.DOMSample2
  9. {
  10.   
  11. /// <summary>
  12. /// Summary description for Form1.
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.ListBox listBox1;
  17. private System.Windows.Forms.Button button1;
  18. /// <summary>
  19. /// Required designer variable.
  20. /// </summary>
  21. private System.ComponentModel.Container components = null;
  22.     private XmlDocument doc=new XmlDocument();
  23.     
  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.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  58.         | System.Windows.Forms.AnchorStyles.Right);
  59.       this.listBox1.Size = new System.Drawing.Size(336, 238);
  60.       this.listBox1.TabIndex = 0;
  61.       this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
  62.       this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  63.       this.button1.Location = new System.Drawing.Point(136, 264);
  64.       this.button1.TabIndex = 1;
  65.       this.button1.Text = "Load XML";
  66.       this.button1.Click += new System.EventHandler(this.button1_Click);
  67.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  68.       this.ClientSize = new System.Drawing.Size(339, 320);
  69.       this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1,
  70.                                                                   this.listBox1});
  71.       this.Text = "Form1";
  72.     }
  73. #endregion
  74. /// <summary>
  75. ///  The main entry point for the application.
  76. /// </summary>
  77. [STAThread]
  78. static void Main() 
  79. {
  80. Application.Run(new Form1());
  81. }
  82.     // DOMSample2/form1.cs
  83.     private void button1_Click(object sender, System.EventArgs e) {
  84.       //doc is declared at the module level
  85.       //change path to math your path structure
  86.       doc.Load("..\..\..\books.xml");
  87.       //get onley the nodes that we want.
  88.       XmlNodeList nodeLst=doc.SelectNodes("/bookstore/book/title");
  89.       //iterate through the XmlNodeList
  90.       foreach(XmlNode node in nodeLst)
  91.         listBox1.Items.Add(node.InnerText);
  92.     }
  93.     private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  94.     {
  95.       //create XPath search string
  96.       string srch="bookstore/book[title='" + listBox1.SelectedItem.ToString() + "']";
  97.       //look for the extra data
  98.       XmlNode foundNode=doc.SelectSingleNode(srch);
  99.       if(foundNode!=null)
  100.         MessageBox.Show(foundNode.InnerText);
  101.       else
  102.         MessageBox.Show("Not found");
  103.       
  104.     }
  105. }
  106. }