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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。