usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Transactions; usingSystem.Xml.Linq; usingSystem.Xml.XPath; usingXmlDAL.Interface; namespaceXmlDAL.DefaultImplementation { ///<summary> ///passworkingnodeparentandpathandworkingnode ///e.g.wanttoworkingwithconfig/menu/menuItem: ///parentpathshouldbe./menu ///workingnodeismenuItem ///</summary> publicclassXmlOperation:IXmlOperation { privatereadonlyXDocument_document; privatereadonlystring_path; privatereadonlyXElement_rootElement; privatereadonlystring_workingNodeParentPath; privatereadonlystring_workingNode; publicXmlOperation(stringfilePath,stringworkingNodeParentPath,stringworkingNode) { _path=filePath; _document=XDocument.Load(filePath); _rootElement=_document.Root; _workingNodeParentPath=workingNodeParentPath; _workingNode=workingNode; } publicIXmlOperationSaveElement(XElementelement) { varret= _rootElement.XPathSelectElements(_workingNodeParentPath) .First() .Descendants(_workingNode) .Where(e=>e.Attribute("id").Value==element.Attribute("id").Value); if(!ret.Any()) returnthis; using(varscope=newTransactionScope()) { ret.Remove(); _rootElement.XPathSelectElements(_workingNodeParentPath).First().Add(element); _rootElement.Save(_path); scope.Complete(); } returnthis; } publicXDocumentDocument { get{return_document;} } publicIXmlOperationAppendToLast(XElementelement) { using(varscope=newTransactionScope()) { _rootElement.XPathSelectElements(_workingNodeParentPath).First().Add(element); _rootElement.Save(_path); scope.Complete(); } returnthis; } publicIXmlOperationRemoveWhere(Func<XElement,bool>condition) { varret=_rootElement.XPathSelectElements(_workingNodeParentPath).First().Descendants(_workingNode).Where(condition); if(!ret.Any()) returnthis; using(varscope=newTransactionScope()) { ret.Remove(); _rootElement.Save(_path); scope.Complete(); } returnthis; } publicIEnumerable<XElement>SearchBy(Func<XElement,bool>condition) { return_rootElement.XPathSelectElements(_workingNodeParentPath).First().Descendants(_workingNode).Where(condition); } } }
Usage :
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Linq.Expressions; usingSystem.Reflection; usingSystem.Transactions; usingCommon.Constant; usingWorkTrackerBC.Interface.WorkItem; usingXmlDAL.DefaultImplementation; usingXmlDAL.Interface; usingSystem.Xml.Linq; usingCommon.Helper; usingEntity.Business; usingSystem.Collections.ObjectModel; namespaceWorkTrackerBC.DefaultImplementation.WorkItem { publicclassWorkItemDomainModel:IWorkItemOperation { privatereadonlyIXmlOperation_xmlOperation; publicWorkItemDomainModel() { //hereacceptxPathtoindicateworkingpath _xmlOperation=newXmlOperation(ConfigurationConst.XmlPathWorkTracker,".","workItem"); } publicObservableCollection<Entity.Business.WorkItem>AppendWorkItem(Entity.Business.WorkItemitem) { varnewElement=WorkItemToElement(item); _xmlOperation.AppendToLast(newElement); returnAll(); } publicObservableCollection<Entity.Business.WorkItem>All() { returnnewObservableCollection<Entity.Business.WorkItem>(_xmlOperation.SearchBy(e=>true).Select(ElementToWorkItem)); } publicObservableCollection<Entity.Business.WorkItem>Where(Expression<Func<Entity.Business.WorkItem,bool>>whereExp) { returnnewObservableCollection<Entity.Business.WorkItem>(All().Where(whereExp.Compile())); } publicIWorkItemOperationUpdateItemById(GuidoldItemId,Entity.Business.WorkItemnewItem) { varnewElement=WorkItemToElement(newItem); if(!_xmlOperation.SearchBy(e=>newGuid(e.Attribute("id").Value)==oldItemId).Any()) returnthis; using(vartransaction=newTransactionScope(TransactionScopeOption.required,TimeSpan.FromMinutes(1))) { _xmlOperation.RemoveWhere(e=>newGuid(e.Attribute("id").Value)==oldItemId); _xmlOperation.AppendToLast(newElement); transaction.Complete(); } returnthis; } publicIWorkItemOperationRemoveById(Guidid) { _xmlOperation.RemoveWhere(e=>newGuid(e.Attribute("id").Value)==id); returnthis; } publicIWorkItemOperationMarkAsDone(Guidid) { varelement=_xmlOperation.SearchBy(e=>newGuid(e.Attribute("id").Value)==id).First(); element.Attribute("isFinished").SetValue(Boolean.TrueString); _xmlOperation.SaveElement(element); returnthis; } publicIWorkItemOperationMarkAsToDo(Guidid) { varelement=_xmlOperation.SearchBy(e=>newGuid(e.Attribute("id").Value)==id).First(); element.Attribute("isFinished").SetValue(Boolean.FalseString); _xmlOperation.SaveElement(element); returnthis; } publicObservableCollection<Entity.Business.WorkItem>GetToDoListBy(Func<Entity.Business.WorkItem,bool>exp) { vartodoAll=GetToDoListAll(); returnnewObservableCollection<Entity.Business.WorkItem>(todoAll.Where(exp)); } publicObservableCollection<Entity.Business.WorkItem>GetToDoListAll() { returnnewObservableCollection<Entity.Business.WorkItem>(_xmlOperation.SearchBy(e=>e.Attribute("isFinished").Value==Boolean.FalseString).Select(ElementToWorkItem)); } publicObservableCollection<Entity.Business.WorkItem>GetDoneBy(Func<Entity.Business.WorkItem,bool>exp) { varallDone=GetDoneAll(); returnnewObservableCollection<Entity.Business.WorkItem>(allDone.Where(exp)); } publicObservableCollection<Entity.Business.WorkItem>GetDoneAll() { returnnewObservableCollection<Entity.Business.WorkItem>(_xmlOperation.SearchBy(e=>e.Attribute("isFinished").Value==Boolean.TrueString).Select(ElementToWorkItem)); } privateXElementWorkItemToElement(Entity.Business.WorkItemitem) { returnnewXElement("workItem",newXElement("desc",item.Desc),newXAttribute("id",item.Id),newXAttribute("name",item.Name),newXAttribute("workType",EnumHelper.GetEnumNameFrom(item.WorkType)),newXAttribute("createDate",item.CreateDate),newXAttribute("isFinished",item.IsFinished?Boolean.TrueString:Boolean.FalseString)); } privateEntity.Business.WorkItemElementToWorkItem(XElementelement) { WorkItemTypeworkType; if(!Enum.TryParse(element.Attribute("workType").Value,outworkType))returnnull; varxElement=element.Element("desc"); varstrDesc=string.Empty; if(xElement!=null) strDesc=xElement.Value; returnnewEntity.Business.WorkItem { Id=newGuid(element.Attribute("id").Value),Name=element.Attribute("name").Value,CreateDate=DateTime.Parse(element.Attribute("createDate").Value),Desc=strDesc,WorkType=workType,IsFinished=bool.Parse(element.Attribute("isFinished").Value) }; } } }
Xml:
<workItems>
<workItem id="" name ="" createDate="" isFinished="">
<desc></desc>
</workItem>
</workItems>