文件转换(待完善)

using System.IO;
using System.Reflection;
using System.Xml;

  

FileStream refs = new FileStream("url.xls", FileMode.Open, FileAccess.Read);
        byte[] infbytes = new byte[(int)refs.Length];
        refs.Read(infbytes, 0, infbytes.Length);
        refs.Close();
//将xls文件转换为byte 字节 url.xls读取路径
FileStream Wrfs = new FileStream("url.dbf", FileMode.Create, FileAccess.Write);
        Wrfs.Write(infbytes, 0, infbytes.Length);
        Wrfs.Close();
//将byte字节转换dbf文件 url.dbf保存路径

  

Common.DBFFile BC = new Common.DBFFile();
        BC.Open("url.dbf");
        DataSet ds = BC.GetDataSet();
        BC.Close();
//打开dbf文件转换DataSet Common 打开路径 

  

string xml_path = HttpContext.Current.Server.MapPath(file_name);
         ds.WriteXml(xml_path);
         ds.Dispose();
//DataSet 转换XML file_name文件名

  

            string path = HttpContext.Current.Server.MapPath(xml_name);
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            string strXmlTxt = xml.InnerText;
            string strXml = xml.InnerXml; //用这种
            DataSet ds = new DataSet();
            if (!string.IsNullOrEmpty(strXml))
            {
                StringReader StrStream = null;
                XmlTextReader Xmlrdr = null;
                try
                {
                    //读取字符串中的信息
                    StrStream = new StringReader(strXml);
                    //获取StrStream中的数据
                    Xmlrdr = new XmlTextReader(StrStream);
                    //ds获取Xmlrdr中的数据                
                    ds.ReadXml(Xmlrdr);
                }
                catch (Exception)
                {
                }
                finally
                {
                    //释放资源
                    if (Xmlrdr != null)
                    {
                        Xmlrdr.Close();
                        StrStream.Close();
                        StrStream.Dispose();
                    }
                }
            }

  

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Common
{


    /**/
    /// <summary>
    /// .dbf 文件的文件头信息类
    /// </summary>
    internal class DBFHeader
    {
        public const int DBFHeaderSize = 32;
        /**/
        /* 版本标志
                 0x02    FoxBASE  
                0x03    FoxBASE+/dBASE III PLUS,无备注  
                0x30    Visual FoxPro  
                0x43    dBASE IV SQL 表文件,无备注  
                0x63    dBASE IV SQL 系统文件,无备注  
                0x83    FoxBASE+/dBASE III PLUS,有备注  
                0x8B    dBASE IV 有备注  
                0xCB    dBASE IV SQL 表文件,有备注  
                0xF5    FoxPro 2.x(或更早版本)有备注  
                0xFB    FoxBASE  
    */
        public sbyte Version;
        /**/
        /* 最后更新年 */
        public byte LastModifyYear;
        /**/
        /* 最后更新月 */
        public byte LastModifyMonth;
        /**/
        /* 最后更新日 */
        public byte LastModifyDay;
        /**/
        /* 文件包含的总记录数 */
        public uint RecordCount;
        /**/
        /* 第一条记录的偏移值,这个值也可以表示文件头长度 */
        public ushort HeaderLength;
        /**/
        /* 记录长度,包括删除标志*/
        public ushort RecordLength;
        /**/
        /* 保留 */
        public byte[] Reserved = new byte[16];
        /**/
        /* 表的标志
                 0x01具有 .cdx 结构的文件
                0x02文件包含备注
                0x04文件是数据库(.dbc) 
                标志可OR 
    */
        public sbyte TableFlag;
        /**/
        /* 代码页标志 */
        public sbyte CodePageFlag;
        /**/
        /* 保留 */
        public byte[] Reserved2 = new byte[2];
    }
    internal class DBFField
    {
        public const int DBFFieldSize = 32;
        /**/
        /* 字段名称 */
        public byte[] Name = new byte[11];
        /**/
        /* 字段类型 C - 字符型  
                Y - 货币型  
                N - 数值型  
                F - 浮点型  
                D - 日期型  
                T - 日期时间型  
                B - 双精度型  
                I - 整型  
                L - 逻辑型 
                M - 备注型  
                G - 通用型  
                C - 字符型(二进制) 
                M - 备注型(二进制) 
                P - 图片型  
    */
        public sbyte Type;
        /**/
        /* 字段偏移量 */
        public uint Offset;
        /**/
        /* 字段长度 */
        public byte Length;
        /**/
        /* 浮点数小数部分长度 */
        public byte Precision;
        /**/
        /* 保留 */
        public byte[] Reserved = new byte[2];
        /**/
        /* dBASE IV work area id */
        public sbyte DbaseivID;
        /**/
        /* */
        public byte[] Reserved2 = new byte[10];
        /**/
        /* */
        public sbyte ProductionIndex;
    }
    /**/
    /// <summary>
    /// .dbf文件操作类
    /// </summary>
    public class DBFFile : IDisposable
    {
        private const string MSG_OPEN_FILE_FAIL = "不能打开文件{0}";

        private bool _isFileOpened;
        private byte[] _recordBuffer;
        private DBFField[] _dbfFields;
        private System.IO.FileStream _fileStream = null;
        private System.IO.BinaryReader _binaryReader = null;
        private string _fileName = string.Empty;
        private uint _fieldCount = 0;
        private int _recordIndex = -1;
        private uint _recordCount = 0;
        private DBFHeader _dbfHeader = null;
        private string _tableName = string.Empty;

        /**/
        /// <summary>
        /// 构造函数
        /// </summary>
        public DBFFile()
        {
        }

        /**/
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="fileName"></param>
        public DBFFile(string fileName)
        {
            if (null != fileName && 0 != fileName.Length)
                this._fileName = fileName;
        }

        /**/
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                this._recordBuffer = null;
                this._dbfHeader = null;
                this._dbfFields = null;

                if (this.IsFileOpened && null != this._fileStream)
                {
                    this._fileStream.Close();
                    this._binaryReader.Close();
                }
                this._fileStream = null;
                this._binaryReader = null;

                this._isFileOpened = false;
                this._fieldCount = 0;
                this._recordCount = 0;
                this._recordIndex = -1;
            }
        }

        /**/
        /// <summary>
        /// 打开dbf文件
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            try
            {
                return this.Open(null);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /**/
        /// <summary>
        /// 打开dbf文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool Open(string fileName)
        {
            if (null != fileName)
                this._fileName = fileName;

            bool ret = false;

            try
            {
                if (!this.OpenFile())
                {
                    // 不能打开dbf文件,抛出不能打开文件异常
                    throw new Exception(string.Format(MSG_OPEN_FILE_FAIL, this._fileName));
                }

                // 读取文件头信息
                ret = this.ReadFileHeader();

                // 读取所有字段信息
                if (ret)
                    ret = this.ReadFields();

                // 分配记录缓冲区
                if (ret && null == this._recordBuffer)
                {
                    this._recordBuffer = new byte[this._dbfHeader.RecordLength];

                    if (null == this._recordBuffer)
                        ret = false;
                }

                // 如果打开文件或读取信息不成功,关闭dbf文件
                if (!ret)
                    this.Close();
            }
            catch (Exception e)
            {
                throw e;
            }

            // 设置当前记录索引为
            this._recordIndex = -1;

            // 返回打开文件并且读取信息的成功状态
            return ret;
        }
        /// <summary>
        /// 将字段类型转换为系统数据类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private Type FieldTypeToColumnType(sbyte type)
        {
            switch (type)
            {
                // C - 字符型、字符型(二进制)
                case (sbyte)'C':
                    return typeof(System.String);

                // Y - 货币型
                case (sbyte)'Y':
                    return typeof(System.Decimal);    // 虽然dbf中'Y'长度为64位,但是Double的精度不够,所以指定Decimal

                // N - 数值型
                case (sbyte)'N':
                    return typeof(System.Decimal);    // dbf中'N'的精度可以达到19,所以用Decimal

                // F - 浮点型
                case (sbyte)'F':
                    return typeof(System.Decimal);    // dbf中'F'的精度可以达到19,所以用Decimal

                // D - 日期型
                case (sbyte)'D':
                    return typeof(System.DateTime);

                // T - 日期时间型
                case (sbyte)'T':
                    return typeof(System.DateTime);

                // B - 双精度型
                case (sbyte)'B':
                    return typeof(System.Double);

                // I - 整型
                case (sbyte)'I':
                    return typeof(System.Int32);

                // L - 逻辑型
                case (sbyte)'L':
                    return typeof(System.Boolean);

                // M - 备注型、备注型(二进制)
                case (sbyte)'M':
                    return typeof(System.String);

                // G - 通用型
                case (sbyte)'G':
                    return typeof(System.String);

                // P - 图片型
                case (sbyte)'P':
                    return typeof(System.String);

                // 缺省字符串型
                default:
                    return typeof(System.String);

            }
        }
        /**/
        /// <summary>
        /// 获取dbf表文件对应的DataSet
        /// </summary>
        /// <returns></returns>
        public System.Data.DataSet GetDataSet()
        {
            // 确保文件已经打开
            if (!this.IsFileOpened || (this.IsBOF && this.IsEOF))
                return null;

            // 构造表格
            System.Data.DataSet ds = new System.Data.DataSet();
            System.Data.DataTable dt = new System.Data.DataTable(this._tableName );

            try
            {
                // 添加表格列
                for (uint i = 0; i < this._fieldCount; i++)
                {
                    System.Data.DataColumn col = new System.Data.DataColumn();
                    string colText = string.Empty;

                    // 获取并设置列标题
                    if (this.GetFieldName(i, ref colText))
                    {
                        col.ColumnName = colText;
                        col.Caption = colText;
                    }

                    // 设置列类型
                    col.DataType = FieldTypeToColumnType(this._dbfFields[i].Type);
                   

                    // 添加列信息
                    dt.Columns.Add(col);
                }

                // 添加所有的记录信息
                this.MoveFirst();

                while (!this.IsEOF)
                {
                    // 创建新记录行
                    System.Data.DataRow row = dt.NewRow();

                    // 循环获取所有字段信息,添加到新的记录行内
                    for (uint i = 0; i < this._fieldCount; i++)
                    {
                        string temp = string.Empty;

                        // 获取字段值成功后才添加到记录行中
                        if (this.GetFieldValue(i, ref temp))
                        {
                            // 如果获取的字段值为空,设置DataTable里字段值为DBNull
                            //                            if (string.Empty != temp)
                            row[(int)i] = temp;
                            //                            else
                            //                                row[(int)i] = System.DBNull.Value;
                        }

                    }

                    // 添加记录行
                    dt.Rows.Add(row);

                    // 后移记录
                    this.MoveNext();
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            ds.Tables.Add(dt);
            return ds;
        }

        /**/
        /// <summary>
        /// 获取相应索引序号处的字段名称
        /// </summary>
        /// <param name="fieldIndex"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public bool GetFieldName(uint fieldIndex, ref string fieldName)
        {
            // 确保文件已经打开
            if (!this.IsFileOpened)
                return false;

            // 索引边界检查
            if (fieldIndex >= this._fieldCount)
            {
                fieldName = string.Empty;
                return false;
            }

            try
            {
                // 反解码
                fieldName = System.Text.Encoding.Default.GetString(this._dbfFields[fieldIndex].Name);
                //去掉末尾的空字符标志
                int i = fieldName.IndexOf('