设为首页 加入收藏

TOP

以对象的方式来访问xml数据表(二)(三)
2019-09-03 03:27:41 】 浏览:118
Tags:对象 方式 访问 xml 数据
ag
= true; } } return okFlag; } } catch (Exception e) { throw e; //return false; } } //修改记录(编号索引:方法一) public bool ModifyNote(string no, params string[] propertyValues) { try { if (no == "" || propertyValues.Length != xmlProperties.Length) { return false; } bool okFlag = false; if (Notes.Elements(noteName).Count() == 0) return false;//数据文件内容为空 var proNote = Notes.Elements(noteName).Attributes("No").SingleOrDefault(m => m.Value == no); if (proNote != null) { var proSubNotes = proNote.Parent.Elements(); int i = 0; foreach (var item in proSubNotes) { item.Value = propertyValues[i++]; } okFlag = true; } return okFlag; } catch (Exception e) { throw e; //return false; } } //修改记录(编号索引:方法二用一个新的节点(No值不变)替代) public bool ModifyNote(string no, XElement noteModified) { try { if (no == "" || noteModified.Elements().Count() != xmlProperties.Length) { return false; } bool okFlag = false; if (Notes.Elements(noteName).Count() == 0) return false;//数据文件内容为空 var proNote = Notes.Elements(noteName).Attributes("No").SingleOrDefault(m => m.Value == no); if (proNote != null) { proNote.Parent.ReplaceWith(noteModified); } return okFlag; } catch (Exception e) { throw e; //return false; } } //查询记录(单一索引) public IEnumerable<XElement> QueryNote(string no = "", params string[] propertyValues) { IEnumerable<XElement> result = null; try { if (no == "" && propertyValues.Length == 0)//返回所有数据 { return Notes.Elements(noteName); } if (no == "" && propertyValues.Length != 0) { for (int i = 0; i < propertyValues.Length; i++) { if (propertyValues[i] == "") continue; if (Notes.Elements(noteName).Count() == 0) return result;//数据文件内容为空 var proNotes = Notes.Elements(noteName).Elements(xmlProperties[i]).Where(m => m.Value == propertyValues[i]); return proNotes; } } else { if (Notes.Elements(noteName).Count() == 0) return result;//数据文件内容为空 var proNote = Notes.Elements(noteName).Attributes("No").SingleOrDefault(m => m.Value == no); if (proNote != null) { result = new XElement[] { proNote.Parent }; } } return result; } catch (Exception e) { throw e; //return false; } } //获取记录的条数 public int Count() { try { return Notes.Elements(noteName).Count(); } catch (Exception e) { throw e; //return false; } } //获取所有记录 public IEnumerable<XElement> AllNotes() { try { return Notes.Elements(noteName); } catch (Exception e) { throw e; //return false; } } //获取最后一条记录的No public int GetLastNoteNo() { try { if (Notes.Elements(noteName).Count() > 0) return (from Note in Notes.Elements(noteName) select Convert.ToInt32(Note.Attribute("No").Value)).Max(); else return 0; } catch (Exception e) { throw e; //return false; } } #endregion } } View Code

  后面自己又用xml文件作为数据库开发了一个WPF桌面应用程序和一个小型的网站,此时的动态链接库还没有什么大的改进,只是对其中的代码进行了一些优化。直到那一天,我在用ASP.NET MVC开发工作室的门户网站(此时不再是用xml文件作为数据库,而是用的SQL Sever),涉及到对网站后台数据库的访问时,我发现了Entity Framework访问数据库的方便简洁之处,首先直接在Model里面写一个能够映射一张数据表的类(一般只需包含对应的属性即可),然后使用数据库上下文接口DbContext来轻轻松松访问数据库。先看看代码:

  Mo

首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在Visual Studio 2019中开启预览.. 下一篇运算符

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目