using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;

using System.Xml.Linq;
using System.Linq;
using System.Diagnostics;

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

      private void button1_Click(object sender, EventArgs e)
      {
	     XmlDocument xml = new XmlDocument();
		 xml.Load(Application.StartupPath + "\\family.xml");
         xml.Save(Application.StartupPath + "\\temp.xml");
      }

      private void button2_Click(object sender, EventArgs e)
      {
         XmlDocument xml = new XmlDocument();
         xml.Load(Application.StartupPath + "\\family.xml");

         XmlNodeList nodeList = xml.GetElementsByTagName("member");

         foreach (XmlNode node in nodeList)
         {
            XmlElement xmlElement = xml.CreateElement("name2");
            xmlElement.InnerText = "empty";

            node.AppendChild(xmlElement);
         }

         xml.Save(Application.StartupPath + "\\temp.xml");
      }

      private void button3_Click(object sender, EventArgs e)
      {
         XmlDocument xml = new XmlDocument();
         xml.Load(Application.StartupPath + "\\family.xml");

         XmlNodeList nodeList = xml.GetElementsByTagName("name");

         foreach (XmlNode node in nodeList)
         {
            XmlAttribute xmlAttribute = xml.CreateAttribute("gender");
            xmlAttribute.Value = "not spec.";

            node.Attributes.Append(xmlAttribute);
         }

         xml.Save(Application.StartupPath + "\\temp.xml");
      }

      private void button4_Click(object sender, EventArgs e)
      {
         XmlDocument xml = new XmlDocument();
         xml.Load(Application.StartupPath + "\\family.xml");

         XmlNodeList nodeList = xml.GetElementsByTagName("name");

         foreach (XmlNode node in nodeList)
         {
            textBox1.Text += node.InnerText + "\r\n";            
         }

         nodeList = xml.GetElementsByTagName("member");

         foreach (XmlNode node in nodeList)
         {
            textBox1.Text += node.Attributes["no"].Value + "\r\n";
         }
      }

      private void button5_Click(object sender, EventArgs e)
      {
         XmlReaderSettings settings = new XmlReaderSettings();
         settings.DtdProcessing = DtdProcessing.Ignore;
         settings.ValidationType = ValidationType.DTD;

         XmlReader reader = XmlReader.Create(Application.StartupPath + "\\family.xml", settings);

         reader.ReadStartElement("family");
         reader.ReadStartElement("member");
         reader.ReadStartElement("name");

         textBox1.Text += reader.ReadString();

         reader.Close();
      }

      private void button6_Click(object sender, EventArgs e)
      {
         XmlDocument xml = new XmlDocument();
         xml.Load(Application.StartupPath + "\\family.xml");

         XmlNodeList nodeList = xml.GetElementsByTagName("name");

         foreach (XmlNode node in nodeList)
         {
            node.InnerText = "new Name";
         }

         nodeList = xml.GetElementsByTagName("member");

         foreach (XmlNode node in nodeList)
         {
            node.Attributes["no"].Value = "-1";
         }

         xml.Save(Application.StartupPath + "\\temp.xml");
      }

      private void button12_Click(object sender, EventArgs e)
      {
          XElement Film = new XElement("Film",
          new XAttribute("Name", "Quantum of Solace"),
          new XAttribute("Year", 2008),
          new XElement("BondGirl", "Olga Kurylenko"),
          new XElement("BondGirl", "Gemma Arterton"));

          Film.Save(@"Bond.xml");
      }

      private void createProcList()
      {
          XDocument ProcList = new XDocument(
                     new XDeclaration("1.0", "utf-8", "yes"),
                     new XComment("Top 10 Prozesse"),
                     new XElement("Processes",
                         from proc in Process.GetProcesses().Take(10)
                         orderby proc.ProcessName
                         select new XElement("Process",
                                 new XAttribute("Id", proc.Id),
                                 new XAttribute("Name", proc.ProcessName)
                             )));

          ProcList.Save(@"ProcList.xml");
      }
   }
}