﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace ReadXML
{
    public partial class Form1 : Form
    {
        private XmlTextReader xtr = null;

        public Form1()
        {
            InitializeComponent();

            // Create XmlTextReader object.
            xtr = new XmlTextReader(@"C:\ucenik\Videos.xml");
            // Don't ignore white space.
            xtr.WhitespaceHandling = WhitespaceHandling.All;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (xtr != null)
                xtr.Close();

            Application.Exit();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            StringBuilder str = new StringBuilder("Format: ");

            if (xtr.Read() == true)
            {
                txtNode.Text = xtr.NodeType.ToString();
                txtName.Text = xtr.Name.ToString();
                txtValue.Text = xtr.Value.ToString();

                switch (xtr.NodeType)
                {
                    case XmlNodeType.Element:
                        str.AppendFormat("<{0}>", xtr.Name);
                        break;
                    case XmlNodeType.Text:
                        break;
                    case XmlNodeType.CDATA:
                        str.AppendFormat("<![CDATA[{0}]]>", xtr.Value);
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        str.AppendFormat("<?{0} {1}?>", xtr.Name, xtr.Value);
                        break;
                    case XmlNodeType.Comment:
                        str.AppendFormat("<!--{0}-->", xtr.Value);
                        break;
                    case XmlNodeType.XmlDeclaration:
                        //Console.Write("<?xml version='1.0'?>");
                        break;
                    case XmlNodeType.DocumentType:
                        str.AppendFormat("<!DOCTYPE {0} [{1}]", xtr.Name, xtr.Value);
                        break;
                    case XmlNodeType.EntityReference:
                        str.Append(xtr.Name);
                        break;
                    case XmlNodeType.EndElement:
                        str.AppendFormat("</{0}>", xtr.Name);
                        break;
                    case XmlNodeType.Whitespace:
                        //Console.Write("{0}", xtr.Value );
                        break;
                }

                txtDisplay.Text = str.ToString();
            }
            else
            {
                // End of file
                txtValue.Text = "EoF";
                txtName.Text = "EoF";
                txtNode.Text = "Eof";
            }
        }
    }
}
