none
C#: Read XML and add to ListView RRS feed

  • Question

  • Hey everybody

    I'm writing a program for manage all customers we have in our company. I have to add a new customer and if I click on "Add Customer" there should be a customer in the ListView. I save the inputs to an xml file with this code I wrote:

    private void btnAddKunde_Click(object sender, EventArgs e)
            {
                if (!Directory.Exists(@"C:\Users\Römel\Desktop\Save"))
                {
                    Directory.CreateDirectory(@"C:\Users\Römel\Desktop\Save");
                }
    
                else
                {
                }
    
                if (!File.Exists(@"C:\Users\Römel\Desktop\Save\save.xml"))
                {
                    File.Create(@"C:\Users\Römel\Desktop\Save\save.xml");
                }
    
                else
                {
                    XmlTextWriter xwriter = new XmlTextWriter(@"C:\Users\Römel\Desktop\Save\save.xml", Encoding.Unicode);
                    xwriter.WriteStartDocument();
                    xwriter.WriteStartElement("Kundenverwaltung");
    
                    xwriter.WriteStartElement("KundenNr");
                    xwriter.WriteString(txtKundenNr.Text);
                    xwriter.WriteEndElement();
    
                    xwriter.WriteStartElement("Nachname");
                    xwriter.WriteString(txtKundeNachname.Text);
                    xwriter.WriteEndElement();
    
                    xwriter.WriteStartElement("Vorname");
                    xwriter.WriteString(txtKundeVorname.Text);
                    xwriter.WriteEndElement();
    
                    xwriter.WriteStartElement("Adresse");
                    xwriter.WriteString(txtKundeAdresse.Text);
                    xwriter.WriteEndElement();
    
                    xwriter.WriteStartElement("Ort");
                    xwriter.WriteString(txtKundeOrt.Text);
                    xwriter.WriteEndElement();
    
                    xwriter.WriteStartElement("Telefon");
                    xwriter.WriteString(txtKundeTel.Text);
                    xwriter.WriteEndElement();
    
                    xwriter.WriteStartElement("Mail");
                    xwriter.WriteString(txtKundeMail.Text);
                    xwriter.WriteEndElement();
    
                    xwriter.WriteEndDocument();
                    xwriter.Close();
                }
            }

    But now it should go to ListView control. And everytime I start the program, it should read the .xml file and the customers should still be in ListView. Can someone give help?

    Thanks!

    Friday, September 20, 2013 8:47 PM

Answers

  • I changed you code slightly.  I removed an else in your code that was incorrect.  Can you use another control instead of the Listview?  I used a DataGridview in the code below which is easy to bind than a listview.  I loaded the XML into a DtaSet and then used table[0] as the datasource.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            //const string folder = @"C:\Users\Römel\Desktop\Save";
            const string folder = @"C:\temp\Save";
            const string basename = "save.xml";
            string filename = folder + @"\" + basename;
            public Form1()
            {
                InitializeComponent();
                if (Directory.Exists(folder))
                {
                    if (File.Exists(filename))
                    {
                        DataSet flatDataSet = new DataSet();
                        flatDataSet.ReadXml(filename);
                        DataTable table = flatDataSet.Tables[0];
                        dataGridView1.DataSource = table;
                       
                    }
                }
            }
            private void btnAddKunde_Click(object sender, EventArgs e)
            {
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                if (!File.Exists(filename))
                {
                    File.Create(filename);
                }
                XmlTextWriter xwriter = new XmlTextWriter(filename, Encoding.Unicode);
                xwriter.WriteStartDocument();
                xwriter.WriteStartElement("Kundenverwaltung");
                xwriter.WriteStartElement("KundenNr");
                xwriter.WriteString(txtKundenNr.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Nachname");
                xwriter.WriteString(txtKundeNachname.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Vorname");
                xwriter.WriteString(txtKundeVorname.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Adresse");
                xwriter.WriteString(txtKundeAdresse.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Ort");
                xwriter.WriteString(txtKundeOrt.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Telefon");
                xwriter.WriteString(txtKundeTel.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Mail");
                xwriter.WriteString(txtKundeMail.Text);
                xwriter.WriteEndElement();
                xwriter.WriteEndDocument();
                xwriter.Close();
            }
        }
    }

    Saturday, September 21, 2013 9:13 AM
  • Look at the code below.  Notice the class name starts with uppercase and the instance of the class starts with lower case.

    When you call the constructor using new it will clear out all the old data.  So with the DataSet if you have in the code "DataSet ds = new DataSet();" or "ds = new DataSet();" the variable ds will be set to a new instance with no data in the dataset.

    The rows in the datatable each is its own instance of the class DataRow. So you must have multiple instances (one for each row).  So every time you add a row you must use "new", otherwise, the data you are writing will overwrite the old data.

    So when you are seeing one row in the datatable either you clear all the data and then wrote one new row;  or you had only one instance of the datarow and you just over wrote the old data.  Both will appear to have the same results which is only one row of data in the database.

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                 //this line create an instance of the class call myClass
                 //using 'new' calls the cunstructor
                 MyClass myClass = new MyClass();
            }
        }
        class MyClass
        {
            public MyClass()
            {
                 //this is the cunstructor
                 //the function has the same name as the class
            }
        }
    }


    jdweng

    Monday, September 23, 2013 4:07 AM

All replies

  • You need to put into the constructor class or Form Load function the code to read the xml.  If you put it into the constructor add this code after InitializeComponent which will load the file every time th e project is run.  The Load function is run more often : after the show() method is used or when the form new method is used.

       public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
        }


    jdweng

    Friday, September 20, 2013 10:26 PM
  • Thank you for your answer. Yeah, I know that I have to put it into the Form1_Load() function. But I don't know what and how. Can you give me a hint? :)
    Friday, September 20, 2013 10:33 PM
  • I changed you code slightly.  I removed an else in your code that was incorrect.  Can you use another control instead of the Listview?  I used a DataGridview in the code below which is easy to bind than a listview.  I loaded the XML into a DtaSet and then used table[0] as the datasource.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            //const string folder = @"C:\Users\Römel\Desktop\Save";
            const string folder = @"C:\temp\Save";
            const string basename = "save.xml";
            string filename = folder + @"\" + basename;
            public Form1()
            {
                InitializeComponent();
                if (Directory.Exists(folder))
                {
                    if (File.Exists(filename))
                    {
                        DataSet flatDataSet = new DataSet();
                        flatDataSet.ReadXml(filename);
                        DataTable table = flatDataSet.Tables[0];
                        dataGridView1.DataSource = table;
                       
                    }
                }
            }
            private void btnAddKunde_Click(object sender, EventArgs e)
            {
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                if (!File.Exists(filename))
                {
                    File.Create(filename);
                }
                XmlTextWriter xwriter = new XmlTextWriter(filename, Encoding.Unicode);
                xwriter.WriteStartDocument();
                xwriter.WriteStartElement("Kundenverwaltung");
                xwriter.WriteStartElement("KundenNr");
                xwriter.WriteString(txtKundenNr.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Nachname");
                xwriter.WriteString(txtKundeNachname.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Vorname");
                xwriter.WriteString(txtKundeVorname.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Adresse");
                xwriter.WriteString(txtKundeAdresse.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Ort");
                xwriter.WriteString(txtKundeOrt.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Telefon");
                xwriter.WriteString(txtKundeTel.Text);
                xwriter.WriteEndElement();
                xwriter.WriteStartElement("Mail");
                xwriter.WriteString(txtKundeMail.Text);
                xwriter.WriteEndElement();
                xwriter.WriteEndDocument();
                xwriter.Close();
            }
        }
    }

    Saturday, September 21, 2013 9:13 AM
  • Hi. Thank you for your answer. dataGridView is another opportunity but this looks not really good. It just should display the names and if I click on a name it should display the informations in textboxes. In dataGridView it shows all informations but I have to scroll :/ Do you know another way? I think dataView looks really better. But I don't know how to add items by click a button and how to read the xml file so everytime I open the program, all customers are shown... If you understand what I mean :) Do you have another idea?
    Saturday, September 21, 2013 9:49 AM
  • You can bind textboxes to a Datatable.  You can make the DataGridView wider so you don't have to scroll.  There are lots of options once the XML is in the DataSet/DataTable of binding and filling controls on a form.

    jdweng

    Saturday, September 21, 2013 12:44 PM
  • Hmm. Sounds nice, do you have an example? I've never done this before :)
    Saturday, September 21, 2013 2:32 PM
  • Lets find out exactly what you like and don't like.  The DataGridView has lots of formating options so I can look similar to the ListView.  I you gave me a link to a object that you lie the maybe we can get the DatGridView to look better.

    Do you prefer TextBoxes or a View? the Textboxes we will need to bind multiple boxes to the same DataSet.  I seen webpages on this earlier this week and I can find some if we need them.  the Listview is complicated while formating the DataGridView pretty simple and is the easiest of all the options.


    jdweng

    Saturday, September 21, 2013 2:51 PM
  • It should look like this if this is possible with datagridview:

    If I click on "Example Name", it should display every single information in textboxes I typed in when I was adding this customer.

    Example: If I type in informations in textboxes and click on "Kunde hinzufügen" (this means: "Add Customer"), the surname of the customer (here: "Nachname") should display in datagridview and if I click on this surname it should display all the informations I typed in before in texboxes. You understand what I mean? :)

    Saturday, September 21, 2013 3:09 PM
  • Okay, the DataGridView is now like I want it to look like. But if I want to add a new customer, it overrides the old customer. So I can just have 1 customer and this is not what I'm looking for^^ How can I fix this? So I can have more than 1 customer? Oh, and everytime I click the "Add Customer" button, it should add the new customer to the DataGridView. Can you help me? Thanks
    Sunday, September 22, 2013 4:24 PM
  • One of two things are happening.  You are either calling 'new' when you shouldn't or not calling 'new' when you should.

    1) You are not adding a "new" (calling the constructor) row to either the DatGridView Row or the DataTable.

    2) You are caling the constructor on the DataTable by calling "new"


    jdweng

    Sunday, September 22, 2013 4:52 PM
  • Hmm. I don't understand what you mean right now. What do you mean with "new"?
    Sunday, September 22, 2013 6:38 PM
  • Look at the code below.  Notice the class name starts with uppercase and the instance of the class starts with lower case.

    When you call the constructor using new it will clear out all the old data.  So with the DataSet if you have in the code "DataSet ds = new DataSet();" or "ds = new DataSet();" the variable ds will be set to a new instance with no data in the dataset.

    The rows in the datatable each is its own instance of the class DataRow. So you must have multiple instances (one for each row).  So every time you add a row you must use "new", otherwise, the data you are writing will overwrite the old data.

    So when you are seeing one row in the datatable either you clear all the data and then wrote one new row;  or you had only one instance of the datarow and you just over wrote the old data.  Both will appear to have the same results which is only one row of data in the database.

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                 //this line create an instance of the class call myClass
                 //using 'new' calls the cunstructor
                 MyClass myClass = new MyClass();
            }
        }
        class MyClass
        {
            public MyClass()
            {
                 //this is the cunstructor
                 //the function has the same name as the class
            }
        }
    }


    jdweng

    Monday, September 23, 2013 4:07 AM