Mega Code Archive

 
Categories / C# / GUI Windows Form
 

Fill XML data to ListBox

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Xml; class Form1 : Form {     public Form1() {         InitializeComponent();     }     private void buttonLoopThroughDocument_Click(object sender, EventArgs e) {         listBoxXmlNodes.Items.Clear();         XmlDocument document = new XmlDocument();         document.Load("Books.xml");         RecurseXmlDocument((XmlNode)document.DocumentElement, 0);     }     private void RecurseXmlDocument(XmlNode root, int indent) {         if (root == null)             return;         if (root is XmlElement){             listBoxXmlNodes.Items.Add(root.Name.PadLeft(root.Name.Length + indent));             if (root.HasChildNodes)                 RecurseXmlDocument(root.FirstChild, indent + 2);             if (root.NextSibling != null)                 RecurseXmlDocument(root.NextSibling, indent);         } else if (root is XmlText) {             string text = ((XmlText)root).Value;             listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));         } else if (root is XmlComment) {             string text = root.Value;             listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));             if (root.HasChildNodes)                 RecurseXmlDocument(root.FirstChild, indent + 2);             if (root.NextSibling != null)                 RecurseXmlDocument(root.NextSibling, indent);         }     }     private void buttonCreateNode_Click(object sender, EventArgs e) {         XmlDocument document = new XmlDocument();         document.Load("Books.xml");         XmlElement root = document.DocumentElement;         XmlElement newBook = document.CreateElement("book");         XmlElement newTitle = document.CreateElement("title");         XmlElement newAuthor = document.CreateElement("author");         XmlElement newCode = document.CreateElement("code");         XmlText title = document.CreateTextNode("C#");         XmlText author = document.CreateTextNode("AAA");         XmlText code = document.CreateTextNode("1234567890");         XmlComment comment = document.CreateComment("comment");         newBook.AppendChild(comment);         newBook.AppendChild(newTitle);         newBook.AppendChild(newAuthor);         newBook.AppendChild(newCode);         newTitle.AppendChild(title);         newAuthor.AppendChild(author);         newCode.AppendChild(code);         root.InsertAfter(newBook, root.FirstChild);         document.Save("Books.xml");     }     private void buttonDeleteNode_Click(object sender, EventArgs e) {         XmlDocument document = new XmlDocument();         document.Load("Books.xml");         XmlElement root = document.DocumentElement;         if (root.HasChildNodes) {             XmlNode book = root.LastChild;             root.RemoveChild(book);             document.Save("Books.xml");         }     }     private void buttonSelect_Click(object sender, EventArgs e) {         XmlDocument document = new XmlDocument();         document.Load("Books.xml");         XmlElement root = document.DocumentElement;         XmlNodeList nodeList = root.SelectNodes("//book[@pages='1000']");         foreach (XmlNode n in nodeList) {             MessageBox.Show(n.InnerText);         }     }     private void InitializeComponent() {         this.buttonLoopThroughDocument = new System.Windows.Forms.Button();         this.listBoxXmlNodes = new System.Windows.Forms.ListBox();         this.buttonCreateNode = new System.Windows.Forms.Button();         this.buttonDeleteNode = new System.Windows.Forms.Button();         this.buttonSelect = new System.Windows.Forms.Button();         this.SuspendLayout();         //          // buttonLoopThroughDocument         //          this.buttonLoopThroughDocument.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));         this.buttonLoopThroughDocument.Location = new System.Drawing.Point(444, 13);         this.buttonLoopThroughDocument.Name = "buttonLoopThroughDocument";         this.buttonLoopThroughDocument.TabIndex = 0;         this.buttonLoopThroughDocument.Text = "Loop";         this.buttonLoopThroughDocument.Click += new System.EventHandler(this.buttonLoopThroughDocument_Click);         //          // listBoxXmlNodes         //          this.listBoxXmlNodes.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)                     | System.Windows.Forms.AnchorStyles.Left)                     | System.Windows.Forms.AnchorStyles.Right)));         this.listBoxXmlNodes.FormattingEnabled = true;         this.listBoxXmlNodes.Location = new System.Drawing.Point(13, 13);         this.listBoxXmlNodes.Name = "listBoxXmlNodes";         this.listBoxXmlNodes.Size = new System.Drawing.Size(424, 225);         this.listBoxXmlNodes.TabIndex = 1;         //          // buttonCreateNode         //          this.buttonCreateNode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));         this.buttonCreateNode.Location = new System.Drawing.Point(444, 43);         this.buttonCreateNode.Name = "buttonCreateNode";         this.buttonCreateNode.TabIndex = 2;         this.buttonCreateNode.Text = "Create Node";         this.buttonCreateNode.Click += new System.EventHandler(this.buttonCreateNode_Click);         //          // buttonDeleteNode         //          this.buttonDeleteNode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));         this.buttonDeleteNode.Location = new System.Drawing.Point(444, 73);         this.buttonDeleteNode.Name = "buttonDeleteNode";         this.buttonDeleteNode.TabIndex = 3;         this.buttonDeleteNode.Text = "Delete Node";         this.buttonDeleteNode.Visible = false;         this.buttonDeleteNode.Click += new System.EventHandler(this.buttonDeleteNode_Click);         //          // buttonSelect         //          this.buttonSelect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));         this.buttonSelect.Location = new System.Drawing.Point(444, 103);         this.buttonSelect.Name = "buttonSelect";         this.buttonSelect.TabIndex = 4;         this.buttonSelect.Text = "Select";         this.buttonSelect.Visible = false;         this.buttonSelect.Click += new System.EventHandler(this.buttonSelect_Click);         //          // Form1         //          this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);         this.ClientSize = new System.Drawing.Size(531, 250);         this.Controls.Add(this.buttonSelect);         this.Controls.Add(this.buttonDeleteNode);         this.Controls.Add(this.buttonCreateNode);         this.Controls.Add(this.listBoxXmlNodes);         this.Controls.Add(this.buttonLoopThroughDocument);         this.Name = "Form1";         this.Text = "Xml Nodes";         this.ResumeLayout(false);     }     private System.Windows.Forms.Button buttonLoopThroughDocument;     private System.Windows.Forms.ListBox listBoxXmlNodes;     private System.Windows.Forms.Button buttonCreateNode;     private System.Windows.Forms.Button buttonDeleteNode;     private System.Windows.Forms.Button buttonSelect;     [STAThread]     static void Main() {         Application.EnableVisualStyles();         Application.Run(new Form1());     } }