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

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.DOMSample3
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.ListBox listBox1;
  16. private System.Windows.Forms.Button button1;
  17. /// <summary>
  18. /// Required designer variable.
  19. /// </summary>
  20. private System.ComponentModel.Container components=null;
  21. private XmlDocument doc=new XmlDocument();
  22. public Form1()
  23. {
  24. //
  25. // Required for Windows Form Designer support
  26. //
  27. InitializeComponent();
  28. //
  29. // TODO: Add any constructor code after InitializeComponent call
  30. //
  31. }
  32. /// <summary>
  33. /// Clean up any resources being used.
  34. /// </summary>
  35. protected override void Dispose( bool disposing )
  36. {
  37. if( disposing )
  38. {
  39. if (components != null) 
  40. {
  41. components.Dispose();
  42. }
  43. }
  44. base.Dispose( disposing );
  45. }
  46. #region Windows Form Designer generated code
  47. /// <summary>
  48. /// Required method for Designer support - do not modify
  49. /// the contents of this method with the code editor.
  50. /// </summary>
  51.     private void InitializeComponent()
  52.     {
  53.       this.listBox1 = new System.Windows.Forms.ListBox();
  54.       this.button1 = new System.Windows.Forms.Button();
  55.       this.SuspendLayout();
  56.       // 
  57.       // listBox1
  58.       // 
  59.       this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  60.         | System.Windows.Forms.AnchorStyles.Right)));
  61.       this.listBox1.Location = new System.Drawing.Point(24, 16);
  62.       this.listBox1.Name = "listBox1";
  63.       this.listBox1.Size = new System.Drawing.Size(232, 225);
  64.       this.listBox1.TabIndex = 0;
  65.       this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
  66.       // 
  67.       // button1
  68.       // 
  69.       this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  70.       this.button1.Location = new System.Drawing.Point(40, 256);
  71.       this.button1.Name = "button1";
  72.       this.button1.Size = new System.Drawing.Size(200, 24);
  73.       this.button1.TabIndex = 1;
  74.       this.button1.Text = "Load XML";
  75.       this.button1.Click += new System.EventHandler(this.button1_Click);
  76.       // 
  77.       // Form1
  78.       // 
  79.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  80.       this.ClientSize = new System.Drawing.Size(288, 293);
  81.       this.Controls.Add(this.button1);
  82.       this.Controls.Add(this.listBox1);
  83.       this.Name = "Form1";
  84.       this.Text = "Form1";
  85.       this.ResumeLayout(false);
  86.     }
  87. #endregion
  88. /// <summary>
  89. ///  The main entry point for the application.
  90. /// </summary>
  91. [STAThread]
  92. static void Main() 
  93. {
  94. Application.Run(new Form1());
  95. }
  96.     // DOMSample3/form1.cs
  97.     private void button1_Click(object sender, System.EventArgs e)
  98.     {
  99.       //change path to match your structure
  100.       doc.Load("..\..\..\books.xml");
  101.       //create a new 'book' element
  102.       XmlElement newBook=doc.CreateElement("book");
  103.       //set some attributes
  104.       newBook.SetAttribute("genre","Mystery");
  105.       newBook.SetAttribute("publicationdate","2001");
  106.       newBook.SetAttribute("ISBN","123456789");
  107.       //create a new 'title' element
  108.       XmlElement newTitle=doc.CreateElement("title");
  109.       newTitle.InnerText="Case of the Missing Cookie";
  110.       newBook.AppendChild(newTitle);
  111.       //create new author element
  112.       XmlElement newAuthor=doc.CreateElement("author");
  113.       newBook.AppendChild(newAuthor);
  114.       //create new name element
  115.       XmlElement newName=doc.CreateElement("name");
  116.       newName.InnerText="Cookie Monster";
  117.       newAuthor.AppendChild(newName);
  118.       //create new price element
  119.       XmlElement newPrice=doc.CreateElement("price");
  120.       newPrice.InnerText="9.95";
  121.       newBook.AppendChild(newPrice);
  122.       //add to the current document
  123.       doc.DocumentElement.AppendChild(newBook);
  124.       //write out the doc to disk
  125.       XmlTextWriter tr=new XmlTextWriter("..\booksEdit.xml",null);
  126.       tr.Formatting=Formatting.Indented;
  127.       doc.WriteContentTo(tr);
  128.      
  129.       tr.Close();
  130.       //load listBox1 with all of the titles, including new one
  131.       XmlNodeList nodeLst=doc.GetElementsByTagName("title");
  132.       
  133.       foreach(XmlNode node in nodeLst)
  134.         listBox1.Items.Add(node.InnerText);
  135.       
  136.     }
  137.     
  138.     private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  139.     {
  140.       string srch="bookstore/book[title='" + listBox1.SelectedItem.ToString() + "']";
  141.       XmlNode foundNode=doc.SelectSingleNode(srch);
  142.       if(foundNode!=null)
  143.         MessageBox.Show(foundNode.InnerText);
  144.       else
  145.         MessageBox.Show("Not found");
  146.       
  147.     }
  148. }
  149. }