I want to create a data access layer, supporting CRUD methods, with an underlying storage of XML files.
I’m new to XML, and I’m not quite sure how to work with XmlDocument, XDocument, XmlSerializer etc..
Here’s my basic idea for a data access class:
public class EmployeesDao
{
private const string FILE_NAME = "file.xml";
//an XDocument which contains all the employees records
private XDocument m_XDocument;
private XmlSerializer m_XmlSerializer;
public TestCasesDao()
{
//is this correct?
m_XDocument = XDocument.Load(@"c:\" + FILE_NAME);
m_XmlSerializer = new XmlSerializer(typeof(EmployeeDTO));
}
public void Save(IEmployee employee)
{
var dto = new EmployeeDTO(employee);
//TODO: serialize the DTO, add it to the XDocument, and save to file
}
public IEmployee GetEmployee(string name)
{
//TODO: retrieve an EmployeeDTO from my XDocument
return employeeDto.Convert(); //return an IEmployee
}
//TODO: update and delete methods...
}
Any ideas as to how to fill in the missing gaps?
For Serialization you can use Generic methods
For Update and Delete You can Retrieve the collection or object from file and edit and overwrite the existing one or you can use XPath expression for directly edit the XML
XML/0340_XPath.htm”>http://www.java2s.com/Tutorial/CSharp/0540_XML/0340_XPath.htm