using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;
using System.Data.Linq.Mapping;

using System.Data;
using System.Data.SqlClient;

using System.Xml.Linq;

namespace LINQ2XML
{
    public class Program
    {
        public static void Main(string[] args)
        {
            #region Einmal ausführen - Erstellt das XML-Dokument
            //DataSet northwind = new DataSet("NorthwindDS");

            //SqlDataAdapter adapterCustomer =
            //    new SqlDataAdapter("select * from Customers;select * from Orders",
            //        @"server=(local)\sqlexpress;database=northwind;integrated security=sspi");

            //adapterCustomer.TableMappings.Add("Table", "Customers");
            //adapterCustomer.Fill(northwind);

            //DataTable customer = northwind.Tables["Customers"];

            //customer.WriteXml(@"customers.xml");
            #endregion

            #region Erzeugen von XML Dokumente mit der LINQ to XML Api
            XDocument srcTree = new XDocument
                (
                    new XComment("Kommentar erzeugen"),
                    new XElement
                        ("Daten",
                            new XElement("Element", "data1", new XAttribute("id", 1)),
                            new XElement("Element", "data2", new XAttribute("id", 2)),
                            new XElement("Element", "data3", new XAttribute("id", 3)),
                            new XElement("Element", "data4", new XAttribute("id", 4))
                        )
                );

            srcTree.Save("Demo.xml");
            #endregion

            #region Abfragen mit Linq to Sql
            XDocument customersXml = XDocument.Load(@"customers.xml");

            var query =
                from c in customersXml.Descendants("Customers")
                where (string)c.Element("Country") == "Germany"
                select c;

            foreach (var item in query)
            {
                if (item.Element("CustomerID").Value == "ALFKI")
                    item.Element("CustomerID").Value = "alfki";

                Console.WriteLine(item.Element("CustomerID").Value);
            }

            customersXml.Save(@"Demo2.xml");
            #endregion
        }
    }
}
