加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Xml Operation Class Summary

发布时间:2020-12-16 05:13:36 所属栏目:百科 来源:网络整理
导读:using System ; using System . Collections . Generic ; using System . Linq ; using System . Transactions ; using System . Xml . Linq ; using System . Xml . XPath ; using XmlDAL . Interface ; namespace XmlDAL . DefaultImplementation { /// su
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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读