Tuesday, January 13, 2009

.NET3.5 load XML class--- ObjectDataSource

public class CustomerDataProvider
{
public CustomerDataProvider(){}
private List Customers
{
get
{
XDocument xdoc = XDocument.Load(@"customers.xml");
List customers = new List();
IEnumerable iCustoms
= from C in xdoc.Descendants("customer")
orderby C.Attribute("CustomerID").Value
select new Customer
{
ID = C.Attribute("CustomerID").Value,
CompanyName = C.Attribute("CompanyName").Value,
ContactName = C.Attribute("ContactName").Value,
Address = C.Attribute("Address").Value,
ContactTitle = C.Attribute("ContactTitle").Value,
City = C.Attribute("City").Value,
Phone = C.Attribute("Phone").Value,
State = C.Attribute("State").Value,
ZipCode = C.Attribute("ZipCode").Value

};
customers = (List)iCustoms;
return customers;

}
}
public IEnumerable FindAll()
{
return this.Customers;
}
public IEnumerable FindById(string CustomerId)
{
return (from C in this.Customers where C.ID == CustomerId select C);
}
public void Update(Customer newCustomer)
{
Customer custm = this.Customers.Find(x => x.ID == newCustomer.ID);
custm.CompanyName = newCustomer.CompanyName;
custm.ContactName = newCustomer.ContactName;
custm.ContactTitle = newCustomer.ContactTitle;
custm.Address = newCustomer.Address;
custm.City = newCustomer.City;
custm.State = newCustomer.State;
custm.Phone = newCustomer.Phone;
custm.ZipCode = newCustomer.ZipCode;
}
}

No comments: