+ Reply to Thread
Results 1 to 3 of 3

Thread: Linq to XML

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    34

    Linq to XML

    Linq to XML , i do not understand the concept , can you post even a quick tutorial as i have exam and i need ideas about how to convert Linq To XML

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    41
    i`ll be doing operations like how to read, insert, update and delete from an XML file.

    firstly , this is an xml file we are going to work on :
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Customers>
     <Customer ID="1">
      <Forename>Joe</Forename>
      <Surname>Stevens</Surname>
      <DOB>31/01/1983</DOB>
      <Location>Sydney</Location>
     </Customer>
     <Customer ID="2">
      <Forename>Tom</Forename>
      <Surname>Male</Surname>
      <DOB>02/02/1977</DOB>
      <Location>Brisbane</Location>
     </Customer>
     <Customer ID="3">
      <Forename>Emily </Forename>
      <Surname>Stevens</Surname>
      <DOB>14/01/1988</DOB>
      <Location>Sydney</Location>
     </Customer>
     <Customer ID="4">
      <Forename>Lee</Forename>
      <Surname>Phipps</Surname>
      <DOB>05/12/1982</DOB>
      <Location>Melbourne</Location>
     </Customer>
     <Customer ID="5">
      <Forename>Saul</Forename>
      <Surname>Stevens</Surname>
      <DOB>02/08/1984</DOB>
      <Location>Perth</Location>
     </Customer>
    </Customers>
    this is a simple list of customers information , i also created a class to represent each customer , below as you can see :
    Code:
    public class Customer
    {
        public int ID { get; set; }
        public string Forename { get; set; }
        public string Surname { get; set; }
        public string DOB { get; set; }
        public string Location { get; set; }
    }
    now lets say i want to select a customer based on his ID , this code is on asp.net :

    Code:
    public static Customer GetCustomer(int customerID)
    {
        XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("~/Data/Customers.xml"));
     
        return (from c in data.Descendants("Customer")
                where c.Attribute("ID").Value.Equals(customerID.ToString())
                select new Customer()
                {
                    ID = Convert.ToInt32(c.Attribute("ID").Value),
                    Forename = c.Element("Forename").Value,
                    Surname = c.Element("Surname").Value,
                    DOB = c.Element("DOB").Value,
                    Location = c.Element("Location").Value
     
                }).FirstOrDefault();
    }

    To get a list of all customers is very similar to getting a single customer, although the method will return a generic list of Customer objects:
    Code:
    public static List<Customer> GetCustomers()
    {
        XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("~/Data/Customers.xml"));
     
        return (from c in data.Descendants("Customer")
                orderby c.Attribute("Surname")
                select new Customer()
                {
                    ID = Convert.ToInt32(c.Attribute("ID").Value),
                    Forename = c.Element("Forename").Value,
                    Surname = c.Element("Surname").Value,
                    DOB = c.Element("DOB").Value,
                    Location = c.Element("Location").Value
     
                }).ToList();
    }
    and here is a single method called Save which is used for both inserting and updating. It accepts a Customer object and performs an insert or update depending on the ID value of that object:
    Code:
    public static void Save(Customer customer)
    {
        XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("~/Data/Customers.xml"));
     
        if (customer.ID > 0)
        {
            XElement customerElement = data.Descendants("Customer").Where(c => c.Attribute("ID").Value.Equals(customer.ID.ToString())).FirstOrDefault();
            if (customerElement != null)
            {
                customerElement.SetElementValue("Forename", customer.Forename);
                customerElement.SetElementValue("Surname", customer.Surname);
                customerElement.SetElementValue("DOB", customer.DOB);
                customerElement.SetElementValue("Location", customer.Location);
     
                data.Save(HttpContext.Current.Server.MapPath("~/Data/Customers.xml"));
            }
        }
        else
        {
            XElement newCustomer = new XElement (   "Customer",
                                                    new XElement("Forename", customer.Forename),
                                                    new XElement("Surname", customer.Surname),
                                                    new XElement("DOB", customer.DOB),
                                                    new XElement("Location", customer.Location)
                                                );
     
            newCustomer.SetAttributeValue("ID", GetNextAvailableID());
     
            data.Element("Customers").Add(newCustomer);
            data.Save(HttpContext.Current.Server.MapPath("~/Data/Customers.xml"));
        }
    }
    The final part of the tutorial is deleting from the XML file:
    Code:
    public static void Delete(Customer customer)
    {
        XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("~/Data/Customers.xml"));
     
        XElement customerElement = data.Descendants("Customer").Where(c => c.Attribute("ID").Value.Equals(customer.ID.ToString())).FirstOrDefault();
        if (customerElement != null)
        {
            customerElement.Remove();
            data.Save(HttpContext.Current.Server.MapPath("~/Data/Customers.xml"));
        }
    }

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    34
    that was wonderful , now i got the overall idea ,

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts