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

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.DomSample1
  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.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  62.       this.button1.Location = new System.Drawing.Point(136, 264);
  63.       this.button1.TabIndex = 1;
  64.       this.button1.Text = "Load XML";
  65.       this.button1.Click += new System.EventHandler(this.button1_Click);
  66.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  67.       this.ClientSize = new System.Drawing.Size(339, 320);
  68.       this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1,
  69.                                                                   this.listBox1});
  70.       this.Text = "Form1";
  71.     }
  72. #endregion
  73. /// <summary>
  74. ///  The main entry point for the application.
  75. /// </summary>
  76. [STAThread]
  77. static void Main() 
  78. {
  79. Application.Run(new Form1());
  80. }
  81.     // DOMSample1/form1.cs
  82.     private void button1_Click(object sender, System.EventArgs e)
  83.     {
  84.       //doc is declared at the module level
  85.       //change path to math your path structure
  86.       doc.Load("..\..\..\books.xml");
  87.       //get only the nodes that we want.
  88.       XmlNodeList nodeLst=doc.GetElementsByTagName("title");
  89.       //iterate through the XmlNodeList
  90.       foreach(XmlNode node in nodeLst)
  91.         listBox1.Items.Add(node.InnerText);
  92.     }
  93. }
  94. }