public void Update(WCFCustomerType customer) { try { XElement customers = XElement.Load(dataFilePath); var customerFNameElements = from cust in customers.Elements("Customer") where (Int32)cust.Attribute("ID") == customer.CustomerID select cust.Element("CustomerFName"); var customerLNameElements = from cust in customers.Elements("Customer") where (Int32)cust.Attribute("ID") == customer.CustomerID select cust.Element("CustomerLName"); // The following will throw InvalidOperationException if // the collections are empty meaning a customer with // the specified ID does not exist. XElement customerFNameElement = customerFNameElements.First(); customerFNameElement.SetValue(customer.FirstName); XElement customerLNameElement = customerLNameElements.First(); customerLNameElement.SetValue(customer.LastName); customers.Save(dataFilePath); } catch (InvalidOperationException) { throw new ObjectNotFoundException( "Unable to update the customer with ID = " + customer.CustomerID.ToString() + ". The customer no longer exists."); } catch (Exception innerExeption) { throw new RuntimeException( "There was a problem updating the customer with ID = " + customer.CustomerID.ToString() + ".", innerExeption); } }
public WCFCustomerType Create(WCFCustomerType newCustomer) { try { XElement customers = XElement.Load(dataFilePath); Int32 nextCustomerId = (Int32)customers.Attribute("NextCustomerId"); WCFCustomerType returnCustomer = new WCFCustomerType(); returnCustomer.CustomerID = nextCustomerId; returnCustomer.FirstName = newCustomer.FirstName; returnCustomer.LastName = newCustomer.LastName; XElement newCustomerElement = new XElement("Customer"); newCustomerElement.SetAttributeValue("ID", nextCustomerId); XElement newCustomerFNameElement = new XElement("CustomerFName", returnCustomer.FirstName); XElement newCustomerLNameElement = new XElement("CustomerLName", returnCustomer.LastName); newCustomerElement.Add(newCustomerFNameElement); newCustomerElement.Add(newCustomerLNameElement); customers.Add(newCustomerElement); customers.SetAttributeValue("NextCustomerId", nextCustomerId + 1); customers.Save(dataFilePath); return(returnCustomer); } catch (Exception generalException) { throw new RuntimeException( "There was a problem creating a new customer.", generalException); } }