XML parser


One thing that we want to consider is using xml as a database almost all of the computer can read the data from xml suppose that we have an xml file having this contents.
<Person>
<Name>
      <LastName>Tolentino </LastName>
      <MiddleName>Aguinaldo</MiddleName>
      <FirstName>JOven</FirstName>
      <Age>24</Age>
     
      <LastName>Tolentino </LastName>
      <MiddleName>Agustin</MiddleName>
      <FirstName>Mariale</FirstName>
      <Age>24</Age>
</Name>

</Person>

The provlem with this is the language the we use to load this one as a database, this article will show on how to load this file on a listview on c#
First is  we need to Import this library using System.Xml; then after that we are ready to go, just place it on the top of your class.
XmlTextReader reader = new XmlTextReader(Application.StartupPath + "\\main.xml");
            XmlNodeType type;

            ListViewItem lv = new ListViewItem();
            while(reader.Read())
            {
                type = reader.NodeType;
                if (type == XmlNodeType.Element)
                {

                    switch (reader.Name.ToLower())
                    {
                        case "lastname":
                            reader.Read();
                            lv = new ListViewItem(reader.Value);
                            break;
                        case "middlename":
                            reader.Read();
                            lv.SubItems.Add(reader.Value);
                            break;
                        case "age":
                            reader.Read();
                            lv.SubItems.Add(reader.Value);
                            this.listView1.Items.Add(lv);

                            break;

                        case "firstname":
                            reader.Read();
                            lv.SubItems.Add(reader.Value);
                            break;
                      

                        default:
                            break;

                    }
                  
                  
                }

           
            }

Suppose that we place this code on form_load it is expected that you have your xml file on your default foulder bin\debug , then the declaration of reader is done, application.startuppath return the location of your file, concatenated to the  file name of your xml file,

The most important thing is the looping in order for it to iterate to the content of the xml file.

Switch case is done to check on where to put the content on the correct column on the listview.
Note that this code reads the data to to bottom



Listview have 4 columns.

Walang komento:

Mag-post ng isang Komento