DOMSample4.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.   
  9. namespace Wrox.ProCSharp.Xml.DOMSample4
  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. public Form1()
  24. {
  25. //
  26. // Required for Windows Form Designer support
  27. //
  28. InitializeComponent();
  29. //
  30. // TODO: Add any constructor code after InitializeComponent call
  31. //
  32. }
  33. /// <summary>
  34. /// Clean up any resources being used.
  35. /// </summary>
  36. protected override void Dispose( bool disposing )
  37. {
  38. if( disposing )
  39. {
  40. if (components != null) 
  41. {
  42. components.Dispose();
  43. }
  44. }
  45. base.Dispose( disposing );
  46. }
  47. #region Windows Form Designer generated code
  48. /// <summary>
  49. /// Required method for Designer support - do not modify
  50. /// the contents of this method with the code editor.
  51. /// </summary>
  52.     private void InitializeComponent()
  53.     {
  54.       this.listBox1 = new System.Windows.Forms.ListBox();
  55.       this.button1 = new System.Windows.Forms.Button();
  56.       this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  57.         | System.Windows.Forms.AnchorStyles.Right);
  58.       this.listBox1.Size = new System.Drawing.Size(336, 238);
  59.       this.listBox1.TabIndex = 0;
  60.       this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
  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.     // DOMSample4/form1.cs
  82.     private void button1_Click(object sender, System.EventArgs e)
  83.     {
  84.       //create the declaration section
  85.       XmlDeclaration newDec=doc.CreateXmlDeclaration("1.0",null,null);
  86.       doc.AppendChild(newDec);
  87.       //create the new root element
  88.       XmlElement newRoot=doc.CreateElement("newBookstore");
  89.       doc.AppendChild(newRoot);
  90.       //create the new 'book' element
  91.       XmlElement newBook=doc.CreateElement("book");
  92.       //create and set the attributes on the 'book' element
  93.       newBook.SetAttribute("genre","Mystery");
  94.       newBook.SetAttribute("publicationdate","2001");
  95.       newBook.SetAttribute("ISBN","123456789");
  96.       //create the 'title' element
  97.       XmlElement newTitle=doc.CreateElement("title");
  98.       newTitle.InnerText="Case of the Missing Cookie";
  99.       newBook.AppendChild(newTitle);
  100.       //create an author element
  101.       XmlElement newAuthor=doc.CreateElement("author");
  102.       newBook.AppendChild(newAuthor);
  103.       //create the name element
  104.       XmlElement newName=doc.CreateElement("name");
  105.       newName.InnerText="C. Monster";
  106.       newAuthor.AppendChild(newName);
  107.       //creat the price element
  108.       XmlElement newPrice=doc.CreateElement("price");
  109.       newPrice.InnerText="9.95";
  110.       newBook.AppendChild(newPrice);
  111.       //append to the 'book' element to the doc
  112.       doc.DocumentElement.AppendChild(newBook);
  113.       //write to disk Note new filename booksEdit.xml
  114.       XmlTextWriter tr=new XmlTextWriter("..\..\..\booksEdit.xml",null);
  115.       tr.Formatting=Formatting.Indented;
  116.       doc.WriteContentTo(tr);
  117.      
  118.       tr.Close();
  119.       //load the title in the listbox
  120.       XmlNodeList nodeLst=doc.GetElementsByTagName("title");
  121.       
  122.       foreach(XmlNode node in nodeLst)
  123.         listBox1.Items.Add(node.InnerText);
  124.       
  125.     }
  126.     
  127.     private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  128.     {
  129.       string srch="newBookstore/book[title='" + listBox1.SelectedItem.ToString() + "']";
  130.       XmlNode foundNode=doc.SelectSingleNode(srch);
  131.       if(foundNode!=null)
  132.         MessageBox.Show(foundNode.InnerText);
  133.       else
  134.         MessageBox.Show("Not found");
  135.       
  136.     }
  137. }
  138. }