Prerequisites:

Goals:

Steps:

  1. Create a new Console Application project.
  2. Add to the project LINQ DataContext for the Northwind database. It can be done by following steps of the 'LINQ to SQL - Generating DataContext' tutorial. It is also possible to copy files from the solution created for the 'LINQ to SQL - Generating DataContext' (northwind_data_context.src.zip):
  3. Add to the Program class a static method creating an XML file with data loaded from the Northwind database. Assume that only the following fields should be exported to the file:
    static string fileName = "northwind.xml";

    static
    void CreateXmlFile()
    {
        
    NorthwindDataContext northwind = new NorthwindDataContext();
        northwind.Log =
    Console.Out;

        
    XDocument xml = new XDocument(
            
    new XComment("Data from the Northwind database"),
            
    new XElement("Customers",
                
    from customer in northwind.Customers
                
    orderby customer.CompanyName
                
    select
                    new XElement("Customer",
                        
    new XElement("CustomerID", customer.CustomerID),
                        
    new XElement("CompanyName", customer.CompanyName),
                        
    new XElement("ContactName", customer.ContactName),
                        
    new XElement("Orders",
                            
    from order in customer.Orders
                            
    orderby order.OrderDate descending
                            select
                                new XElement("Order",
                                    
    new XElement("OrderID", order.OrderID),
                                    
    new XElement("OrderDate", order.OrderDate)
                                )
                        )

                    )
            )
        );

        
    StreamWriter writer = File.CreateText(fileName);
        writer.Write(xml.ToString());
        writer.Close();
    }
    Call this method from the Main method.
    Compile and run the application. Check the output XML file ('northwind.xml' located in the folder of the executable file).
  4. As a second task, write code loading data from the XML file (use the 'northwind.xml' file - just generated). Display on the console a beginning of the loaded data:
    static XDocument ReadXmlFile()
    {
        
    XDocument xml = XDocument.Load(fileName);
        
    string beginning = xml.ToString().Substring(0, 1024);
        
    Console.WriteLine(beginning);
        
    return xml;
    }
    static void Main(string[] args)
    {
        CreateXmlFile();
        XDocument xml = ReadXmlFile();
    }
    <!--Data from the Northwind database-->
    <Customers>
      <Customer>
        <CustomerID>ALFKI</CustomerID>
        <CompanyName>Alfreds Futterkiste</CompanyName>
        <ContactName>Maria Anders</ContactName>
        <Orders>
          <Order>
            <OrderID>11011</OrderID>
            <OrderDate>1998-04-09T00:00:00</OrderDate>
          </Order>
          <Order>
            <OrderID>10952</OrderID>
            <OrderDate>1998-03-16T00:00:00</OrderDate>
          </Order>
          <Order>
            <OrderID>10835</OrderID>
            <OrderDate>1998-01-15T00:00:00</OrderDate>
          </Order>
          <Order>
            <OrderID>10702</OrderID>
            <OrderDate>1997-10-13T00:00:00</OrderDate>
          </Order>
          <Order>
            <OrderID>10692</OrderID>
            <OrderDate>1997-10-03T00:00:00</OrderDate>
          </Order>
          <Order>
            <OrderID>10643</OrderID>
            <OrderDate>1997-08-25T00:00:00</OrderDate>
          </Order>
        </Orders>
      </Customer>
      <Customer>
        <CustomerID>ANATR</CustomerID>
        <CompanyName>Ana Trujil
  5. Find a customer with the biggest number of orders:
    static void FindBestCustomer(XDocument xml)
    {
        
    var bestCustomerElement =
            (
    from customer in xml.Element("Customers").Elements("Customer")
            
    let ordersCount = customer.Element("Orders").Elements("Order").Count()
            
    orderby ordersCount descending
             select new { CompanyName = customer.Element("CompanyName").Value,
                 OrdersCount = ordersCount })
            .First();
        
    Console.WriteLine("Best customer: {0}, {1} orders",
            bestCustomerElement.CompanyName, bestCustomerElement.OrdersCount);
    }
    static void Main(string[] args)
    {
        CreateXmlFile();
        XDocument xml = ReadXmlFile();
        FindBestCustomer(xml);
    }
    Best customer: Save-a-lot Markets, 31 orders
  6. It is time to modify the XML data:
    static void ModifyXml(XDocument xml)
    {
        xml.Element(
    "Customers").Add(
            
    new XElement("Customer",
                
    new XElement("CustomerID", "WUT"),
                
    new XElement("CompanyName", "Warsaw University of Technology"),
                
    new XElement("ContactName", "Krzysztof Mossakowski"),
                
    new XElement("Orders")
            )
        );
        
    Console.WriteLine("WUT customer added");

        
    XElement toDeleteElement =
            xml.Element(
    "Customers").Elements("Customer").Where(
                customer => customer.Element(
    "CompanyName").Value == "Save-a-lot Markets")
            .FirstOrDefault();
        
    if (toDeleteElement == null)
        {
            
    Console.WriteLine("Save-a-lot Markets not found");
        }
        
    else
        {
            toDeleteElement.Remove();
            
    Console.WriteLine("Save-a-lot Markets removed");
        }
    }
    static void Main(string[] args)
    {
        CreateXmlFile();
        XDocument xml = ReadXmlFile();
        FindBestCustomer(xml);
        ModifyXml(xml);
    }
  7. To verify if the customer was removed, repeat searching for a customer with the biggest number:
    static void Main(string[] args)
    {
        CreateXmlFile();
        XDocument xml = ReadXmlFile();
        FindBestCustomer(xml);
        ModifyXml(xml);
        FindBestCustomer(xml);
    }
    Best customer: Save-a-lot Markets, 31 orders
    WUT customer added
    Save-a-lot Markets removed
    Best customer: Ernst Handel, 30 orders
  8. As the last step, save the modified data into the source XML file:
    static void SaveXml(XDocument xml)
    {
        xml.Save(fileName);
    }

    static void Main(string[] args)
    {
        CreateXmlFile();
        
    XDocument xml = ReadXmlFile();
        FindBestCustomer(xml);
        ModifyXml(xml);
        FindBestCustomer(xml);
        SaveXml(xml);
    }

[Source code]