Xml Operation Class Summary
来源:互联网 发布:打开telnet端口 编辑:程序博客网 时间:2024/03/29 04:49
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{ /// <summary> /// pass working node parent and path and working node /// e.g. want to working with config/menu/menuItem : /// parent path should be ./menu /// working node is menuItem /// </summary> public class XmlOperation : IXmlOperation { private readonly XDocument _document; private readonly string _path; private readonly XElement _rootElement; private readonly string _workingNodeParentPath; private readonly string _workingNode; public XmlOperation(string filePath, string workingNodeParentPath, string workingNode) { _path = filePath; _document = XDocument.Load(filePath); _rootElement = _document.Root; _workingNodeParentPath = workingNodeParentPath; _workingNode = workingNode; } public IXmlOperation SaveElement(XElement element) { var ret = _rootElement.XPathSelectElements(_workingNodeParentPath) .First() .Descendants(_workingNode) .Where(e => e.Attribute("id").Value == element.Attribute("id").Value); if (!ret.Any()) return this; using (var scope = new TransactionScope()) { ret.Remove(); _rootElement.XPathSelectElements(_workingNodeParentPath).First().Add(element); _rootElement.Save(_path); scope.Complete(); } return this; } public XDocument Document { get { return _document; } } public IXmlOperation AppendToLast(XElement element) { using (var scope = new TransactionScope()) { _rootElement.XPathSelectElements(_workingNodeParentPath).First().Add(element); _rootElement.Save(_path); scope.Complete(); } return this; } public IXmlOperation RemoveWhere(Func<XElement, bool> condition) { var ret = _rootElement.XPathSelectElements(_workingNodeParentPath).First().Descendants(_workingNode).Where(condition); if (!ret.Any()) return this; using (var scope = new TransactionScope()) { ret.Remove(); _rootElement.Save(_path); scope.Complete(); } return this; } public IEnumerable<XElement> SearchBy(Func<XElement, bool> condition) { return _rootElement.XPathSelectElements(_workingNodeParentPath).First().Descendants(_workingNode).Where(condition); } }}
Usage :
using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Reflection;using System.Transactions;using Common.Constant;using WorkTrackerBC.Interface.WorkItem;using XmlDAL.DefaultImplementation;using XmlDAL.Interface;using System.Xml.Linq;using Common.Helper;using Entity.Business;using System.Collections.ObjectModel; namespace WorkTrackerBC.DefaultImplementation.WorkItem{ public class WorkItemDomainModel : IWorkItemOperation { private readonly IXmlOperation _xmlOperation; public WorkItemDomainModel() { //here accept xPath to indicate working path _xmlOperation = new XmlOperation(ConfigurationConst.XmlPathWorkTracker, ".","workItem"); } public ObservableCollection<Entity.Business.WorkItem> AppendWorkItem(Entity.Business.WorkItem item) { var newElement = WorkItemToElement(item); _xmlOperation.AppendToLast(newElement); return All(); } public ObservableCollection<Entity.Business.WorkItem> All() { return new ObservableCollection<Entity.Business.WorkItem>(_xmlOperation.SearchBy(e => true).Select(ElementToWorkItem)); } public ObservableCollection<Entity.Business.WorkItem> Where(Expression<Func<Entity.Business.WorkItem, bool>> whereExp) { return new ObservableCollection<Entity.Business.WorkItem>(All().Where(whereExp.Compile())); } public IWorkItemOperation UpdateItemById(Guid oldItemId, Entity.Business.WorkItem newItem) { var newElement = WorkItemToElement(newItem); if (!_xmlOperation.SearchBy(e => new Guid(e.Attribute("id").Value) == oldItemId).Any()) return this; using (var transaction = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(1))) { _xmlOperation.RemoveWhere(e => new Guid(e.Attribute("id").Value) == oldItemId); _xmlOperation.AppendToLast(newElement); transaction.Complete(); } return this; } public IWorkItemOperation RemoveById(Guid id) { _xmlOperation.RemoveWhere(e => new Guid(e.Attribute("id").Value) == id); return this; } public IWorkItemOperation MarkAsDone(Guid id) { var element = _xmlOperation.SearchBy(e => new Guid(e.Attribute("id").Value) == id).First(); element.Attribute("isFinished").SetValue(Boolean.TrueString); _xmlOperation.SaveElement(element); return this; } public IWorkItemOperation MarkAsToDo(Guid id) { var element = _xmlOperation.SearchBy(e => new Guid(e.Attribute("id").Value) == id).First(); element.Attribute("isFinished").SetValue(Boolean.FalseString); _xmlOperation.SaveElement(element); return this; } public ObservableCollection<Entity.Business.WorkItem> GetToDoListBy(Func<Entity.Business.WorkItem, bool> exp) { var todoAll = GetToDoListAll(); return new ObservableCollection<Entity.Business.WorkItem>(todoAll.Where(exp)); } public ObservableCollection<Entity.Business.WorkItem> GetToDoListAll() { return new ObservableCollection<Entity.Business.WorkItem>(_xmlOperation.SearchBy(e => e.Attribute("isFinished").Value == Boolean.FalseString).Select(ElementToWorkItem)); } public ObservableCollection<Entity.Business.WorkItem> GetDoneBy(Func<Entity.Business.WorkItem, bool> exp) { var allDone = GetDoneAll(); return new ObservableCollection<Entity.Business.WorkItem>(allDone.Where(exp)); } public ObservableCollection<Entity.Business.WorkItem> GetDoneAll() { return new ObservableCollection<Entity.Business.WorkItem>(_xmlOperation.SearchBy(e => e.Attribute("isFinished").Value == Boolean.TrueString).Select(ElementToWorkItem)); } private XElement WorkItemToElement(Entity.Business.WorkItem item) { return new XElement("workItem", new XElement("desc", item.Desc), new XAttribute("id", item.Id), new XAttribute("name", item.Name), new XAttribute("workType", EnumHelper.GetEnumNameFrom(item.WorkType)), new XAttribute("createDate", item.CreateDate), new XAttribute("isFinished", item.IsFinished ? Boolean.TrueString : Boolean.FalseString)); } private Entity.Business.WorkItem ElementToWorkItem(XElement element) { WorkItemType workType; if (!Enum.TryParse(element.Attribute("workType").Value, out workType)) return null; var xElement = element.Element("desc"); var strDesc = string.Empty; if (xElement != null) strDesc = xElement.Value; return new Entity.Business.WorkItem { Id = new Guid(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>
- Xml Operation Class Summary
- XML Summary
- xml operation
- C++-Class Summary
- UML class diagram summary
- Summary on 20080703: Attention for hibernate operation
- My Class for Directory Operation
- Summary on 20080707: Hibernate operation on many-to-one relation
- Base operation about XMl file
- C#xml<summary>注释 换行、空行
- Summary
- summary
- summary
- Summary
- Summary
- summary
- Summary
- summary
- SQLSERVER和ORACLE 空值排序
- hdu 4790 Just Random 数论
- Custom Entity And XElement Convertion
- JAVA one2Many Many2one Many2Many
- C -- 字符串和字符串函数
- Xml Operation Class Summary
- oracle之动态sql、游标
- 50个c/c++源代码网站
- java反射机制的实现原理
- 数据库获取当前时间
- HDU 4770 Lights Against Dudely 解题报告
- JAVA开发环境搭建(jdk7,ant,tomcat,maven)
- c++ 跨平台封装socket
- 库