using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string data = DateTime.Now.ToLongDateString() + ", " + DateTime.Now.ToLongTimeString() + ": ";     
        XmlDocument xml = XmlDataSource1.GetXmlDocument();

        foreach (XmlElement element1 in xml.DocumentElement.GetElementsByTagName("email"))
        {
            if (element1.GetAttribute("adres") == TextBox1.Text)
            {
                Label1.Text += data + "<FONT COLOR=red>Podany adres już jest zarejestrowany</FONT>" + "<BR>";
                return;
            }
        }
      
        XmlElement element = xml.CreateElement("email");
        XmlAttribute atrybut = xml.CreateAttribute("adres");
        atrybut.Value = TextBox1.Text;
        element.Attributes.Append(atrybut);
        xml.DocumentElement.AppendChild(element);
        
        try
        {            
            XmlDataSource1.Save();            
            Label1.Text += data + "dodałem do listy adres e-mail: " + TextBox1.Text + "<BR>";
            TextBox1.Text = "";
        }
        catch (Exception exc)
        {
            Label1.Text += data + "<FONT COLOR=red>" + exc.Message + "</FONT>" + "<BR>";
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string data = DateTime.Now.ToLongDateString() + ", " + DateTime.Now.ToLongTimeString() + ": ";
        XmlDocument xml = XmlDataSource1.GetXmlDocument();

        bool znaleziony = false;
        foreach (XmlElement element1 in xml.DocumentElement.GetElementsByTagName("email"))
        {
            if (element1.GetAttribute("adres") == TextBox1.Text)
            {
                znaleziony = true;
                xml.DocumentElement.RemoveChild(element1);
                break;
            }
        }

        try
        {
            XmlDataSource1.Save();
            if (znaleziony) Label1.Text += data + "Adres e-mail: " + TextBox1.Text + " został usunięty z listy<BR>";
            else Label1.Text += data + "Adres e-mail: " + TextBox1.Text + " nie był obecny na liście<BR>";
            TextBox1.Text = "";
        }
        catch (Exception exc)
        {
            Label1.Text += data + "<FONT COLOR=red>" + exc.Message + "</FONT>" + "<BR>";
        }
    }
}
