将DataTable、DataSet和类Class对象存入(序列化为)xml;或将xml文件中的数据转化为DataTable、class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml.Serialization;

namespace Dal
{
    public static class XmlHelper
    {
        #region Xml DataTable文件的操作
        /// <summary>
        /// 将表中数据保存到文件夹folderName下的xml文件xmlName;
        /// </summary>
        /// <param name="xmlName">要保存的xml文件名称</param>
        /// <param name="folderName">文件所在路径,但是并不包括文件名称(Application.StartupPath + Path.DirectorySeparatorChar + "FolderName" + Path.DirectorySeparatorChar;)</param>
        /// <param name="dataTable">要保存的数据表</param>
        /// <returns>保存成功返回true,否则是false</returns>
        public static bool WriteTableToXml(string xmlFileName, string serverFilePath, DataTable dataTable, string tableName)
        {
            if (dataTable.TableName != tableName)
            {
                dataTable.TableName = tableName;
            }
            serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
            try
            {
                string directoryName = Path.GetDirectoryName(serverFilePath);//获取文件所在目录
                bool isExists = Directory.Exists(directoryName);//检查该目录是否存在
                if (!isExists)//不存在该文件目录,则创建
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(serverFilePath));
                }
                dataTable.WriteXml(serverFilePath, XmlWriteMode.WriteSchema);
                return true;
            }
            catch
            {
                return false; ;
            }
        }

        /// <summary>
        /// 从xml文件中读数据,返回一个DataTable(无论是XML文件中包含的是DataSet,还是单独的表)
        /// </summary>
        /// <param name="xmlName">xml文件名称</param>
        /// <param name="serverFilePath">xml文件所在路径</param>
        /// <returns>DataTable</returns>
        public static DataTable ReadTableFromXml(string serverFilePath, string xmlFileName, string tableName)
        {
            serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
            bool isExistsPath = File.Exists(serverFilePath);
            DataTable dataTable = new DataTable(tableName);
            try
            {
                if (isExistsPath)
                {
                    dataTable.ReadXml(serverFilePath);
                }
                else
                {
                    dataTable = null;
                }
                return dataTable;
            }
            catch (IOException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region DataSet XML file operate

        public static bool WriteDataSetToXml(string xmlFileName, string serverFilePath, DataSet dataSet, string dataSetName)
        {
            serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(serverFilePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(serverFilePath));
                }
                dataSet.WriteXml(serverFilePath, XmlWriteMode.WriteSchema);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static DataSet ReadDataSetFromXml(string serverFilePath, string xmlFileName, string dataSetName)
        {
            serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
            bool isExistsPath = File.Exists(serverFilePath);
            DataSet dataSet = new DataSet(dataSetName);
            try
            {
                if (isExistsPath)
                {
                    dataSet.ReadXml(serverFilePath);
                }
                else
                {
                    dataSet = null;
                }
                return dataSet;
            }
            catch (IOException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void SerializerXml<T>(string serverFilePath, string xmlFileName, T t)
        {
            serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
            XmlSerializer xs = new XmlSerializer(typeof(T));
            using (Stream stream = new FileStream(serverFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                xs.Serialize(stream, t);
                stream.Close();
            }

        }

        public static T DeserializeXml<T>(string serverFilePath, string xmlFileName)
        {
            serverFilePath = GetFileFullName(serverFilePath, xmlFileName);
            XmlSerializer xs = new XmlSerializer(typeof(T));
            using (Stream stream = new FileStream(serverFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                T p = (T)xs.Deserialize(stream);
                return p;
            }
        }

        private static string GetFileFullName(string serverFilePath, string xmlFileName)
        {
            if (!serverFilePath.EndsWith("//"))
            {
                serverFilePath += "//";
            }
            if (!xmlFileName.EndsWith(".xml"))
            {
                xmlFileName += ".xml";
            }
            serverFilePath += xmlFileName;
            return serverFilePath;
        }
        #endregion
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值