Fill XML data to ListBox : ListBox « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » ListBoxScreenshots 
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(44413);
        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(1313);
        this.listBoxXmlNodes.Name = "listBoxXmlNodes";
        this.listBoxXmlNodes.Size = new System.Drawing.Size(424225);
        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(44443);
        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(44473);
        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(444103);
        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(513);
        this.ClientSize = new System.Drawing.Size(531250);
        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());
    }

}

 
Related examples in the same category
1.Add new item to ListBox (text from TextBox) Add new item to ListBox (text from TextBox)
2.Remove item if one is selected from ListBoxRemove item if one is selected from ListBox
3.Clear all items in a ListBoxClear all items in a ListBox
4.ListBox selected Item changed eventListBox selected Item changed event
5.Add Object to ListBoxAdd Object to ListBox
6.List Box click eventList Box click event
7.Set TopIndex to auto scroll ListBoxSet TopIndex to auto scroll ListBox
8.Form with list, buttonForm with list, button
9.ListBox: font and imageListBox: font and image
10.CheckedListBox Demo 2CheckedListBox Demo 2
11.ListBox Demo 2ListBox Demo 2
12.ListBox and Metafile EnumListBox and Metafile Enum
13.ListBox ObjectsListBox Objects
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.