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

using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

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

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

      private void button1_Click(object sender, EventArgs e)
      {
         XmlDocument xml = new XmlDocument();
         //xml.LoadXml(@"<root><child>data</child></root>");
         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.Prohibit;
         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 button7_Click(object sender, EventArgs e)
      {
         XPathNavigator navigator;
         XPathDocument document;
         XPathNodeIterator iterator;

         string expression = string.Empty;

         document = new XPathDocument(Application.StartupPath + "\\family.xml");

         navigator = document.CreateNavigator();

         expression = "/family/member/hobbies";

         iterator = navigator.Select(expression);

         while (iterator.MoveNext())
            textBox1.Text += iterator.Current.Value + "\r\n";
      }

      private void button8_Click(object sender, EventArgs e)
      {
         XPathNavigator navigator;
         XPathDocument document;

         //Abfragen
         string expression = string.Empty;

         document = new XPathDocument(Application.StartupPath + "\\family.xml");

         navigator = document.CreateNavigator();

         expression = "sum(/family/member/age) div count(/family/member)";

         textBox1.Text += navigator.Evaluate(expression);
      }

      private void button9_Click(object sender, EventArgs e)
      {
         XPathNavigator navigator;
         XPathDocument document;
         XPathNodeIterator iterator;

         string expression = string.Empty;

         document = new XPathDocument(Application.StartupPath + "\\family.xml");

         navigator = document.CreateNavigator();

         expression = "/family/member/hobbies[../age > 10]";

         iterator = navigator.Select(expression);

         while (iterator.MoveNext())
            textBox1.Text += iterator.Current.Value + "\r\n";
      }

      private void button10_Click(object sender, EventArgs e)
      {
         XslCompiledTransform xslt = new XslCompiledTransform();
         xslt.Load(Application.StartupPath + "\\family.xsl");
         xslt.Transform(Application.StartupPath + "\\family.xml", Application.StartupPath + "\\family.html");
      }

      private void button11_Click(object sender, EventArgs e)
      {
            string name;

            // XML-Dokumente in XDocument-Instanz laden
            XDocument xDoc = XDocument.Load(Application.StartupPath + "\\family.xml");
          
            // Suche nach Familienmitglied #2
            XElement person = (from member in xDoc.Root.Elements() where member.Attribute("no").Value == "2" select member).FirstOrDefault();

            // Wurde das Familienmitglied gefunden?
            if (person != null)
            {
                name = person.Element("name").Value;
            }
            else
            {
                name = "{Unbekannt}";
            }

            // Ausgabe
            Debug.Print("Person #2 ist {0}", name);
      }
   }
}