设为首页 加入收藏

TOP

以对象的方式来访问xml数据表(二)(一)
2019-09-03 03:27:41 】 浏览:116
Tags:对象 方式 访问 xml 数据

  为什么要以对象的方式来访问xml数据表?

  还记得,自己是在一次完成师兄布置的任务时接触到了xml,那时候需要用xml来作为数据文件,保存一个简单的图书管理系统的数据。于是就知道了,可以用xml文件来保存数据(而且比用简单的文本文件保存数据规范的多,在访问与读取数据上面都十分方便),就这样使用xml的征程开始了。

  自己做的第一个WPF桌面应用程序——备忘录,就是用xml文件作为数据库。而且那个时候考虑到以后可能会经常使用到xml文件作为数据库,于是乎就写了一个专门用于访问xml文件的动态链接库,这样不仅可以提高代码的重用性(用功一次,获益无穷),而且还提高了软件后期的维护效率(由于规范),动态链接库实现的基本功能:将连接数据文件的过程和检查规范全封装在一个方法里面(而数据表的属性是通过数组传参传递),将对数据的增、删、查、改也全部封装成各种方法,还封装了一些属性等等。但此时的自己还没有面向对象开发的思维,最终在开发时还是以传统的方式去访问的xml数据表(Element(value))。

  这是我第一个版本的访问xml的动态链接库源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;

namespace XmlIDataBase
{
    public class XmlDataBase
    {
        #region 私有字段
        private string xmlFilePath;
        private string[] xmlProperties;
        private string noteName;
        #endregion

        #region 公有字段
        public XElement Notes;
        #endregion

        #region 公有方法
        //连接数据文件
        public bool Connect(string path_, string noteName_, params string[] properties)
        {
            try
            {
                //匹配xml文件路径
                if (!Regex.IsMatch(path_, @"^(?<fpath>([a-zA-Z]:\\)([\s\.\-\w]+\\)*)(?<fname>[\w]+.[\w]+)") || noteName_ == "" || path_.Length < 5 || path_.Substring(path_.Length - 3).ToLower() != "xml")
                {
                    return false;
                }
                noteName = noteName_;//记录每条记录的名称
                xmlFilePath = path_;//记录文件路径

                if (path_.LastIndexOf("\\") > 0)
                {
                    path_ = path_.Substring(0, path_.LastIndexOf("\\"));
                }
                else
                {
                    path_ = "";
                }

                if (path_ != "" && !Directory.Exists(path_))
                {
                    Directory.CreateDirectory(path_);
                    var xmlFile = new StreamWriter(xmlFilePath);
                    xmlFile.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    xmlFile.WriteLine("<" + noteName + "s>");
                    xmlFile.WriteLine("</" + noteName + "s>");
                    xmlFile.Close();
                }
                else
                {
                    if (!File.Exists(xmlFilePath))
                    {
                        var xmlFile = new StreamWriter(xmlFilePath);
                        xmlFile.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        xmlFile.WriteLine("<" + noteName + "s>");
                        xmlFile.WriteLine("</" + noteName + "s>");
                        xmlFile.Close();
                    }
                }

                Notes = XElement.Load(xmlFilePath);
                xmlProperties = new string[properties.Length];
                xmlProperties = properties;//记录每条记录的属性
                return true;
            }
            catch (Exception e)
            {
                throw e;
                //return false;
            }
        }

        //保存数据文件
        public bool SaveChanged()
        {
            try
            {
                Notes.Save(xmlFilePath);
                return true;
            }
            catch (Exception e)
            {
                throw e;
                //return false;
            }
        }

        //添加纪录:添加到末尾(方法一)
        public bool AddNote(params string[] propertyValues)
        {
            try
            {
                if (propertyValues.Length == xmlProperties.Length)
                {
                    if (Notes.Elements(noteName).Count() > 0)
                    {
                        int newNo;
                        var lastNote = from Note in Notes.Elements() select Convert.ToInt32(Note.Attribute("No").Value);
                        newNo = lastNote.Max() + 1;
                        Notes.LastNode.AddAfterSelf(noteName, new XAttribute("No", newNo));
                        for (int i = 0; i < xmlProperties.Length; i++)
                        {
                            if (i == 0)
                            {
                                Notes.Elements().Last().AddFirst(new XElement(xmlProperties[i], propertyValues[i]));
                            }
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在Visual Studio 2019中开启预览.. 下一篇运算符

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目