久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

一個(gè)經(jīng)過改良的XMLHelper(包含了序列化,,反序列化,創(chuàng)建xml文件,,讀取節(jié)點(diǎn),C#對(duì)...

 maoj66 2013-09-05
public class XmlHelper
    {
        public XmlHelper()
        {
        }
 
        public enum XmlType
        {
            File,
            String
        };
 
        /// <summary>
        /// 創(chuàng)建XML文檔
        /// </summary>
        /// <param name="name">根節(jié)點(diǎn)名稱</param>
        /// <param name="type">根節(jié)點(diǎn)的一個(gè)屬性值</param>
        /// <returns></returns>
        /// .net中調(diào)用方法:寫入文件中,則:
        ///          document = XmlOperate.CreateXmlDocument("sex", "sexy");
        ///          document.Save("c:/bookstore.xml");        
        public static XmlDocument CreateXmlDocument( string name, string type )
        {
            XmlDocument doc = null;
            XmlElement rootEle = null;
            try
            {
                doc = new XmlDocument();
                doc.LoadXml( "<" + name + "/>" );
                rootEle = doc.DocumentElement;
                rootEle.SetAttribute( "type", type );
            }
            catch (Exception er)
            {
                throw er;
            }
            return doc;
        }
 
 
        /// <summary>
        /// 讀取數(shù)據(jù)
        /// </summary>
        /// <param name="path">路徑</param>
        /// <param name="node">節(jié)點(diǎn)</param>
        /// <param name="attribute">屬性名,,非空時(shí)返回該屬性值,,否則返回串聯(lián)值</param>
        /// <returns>string</returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Read(path, "/Node", "")
         * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
         ************************************************/
        public static string Read( string path, string node, string attribute )
        {
            string value = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load( path );
                XmlNode xn = doc.SelectSingleNode( node );
                value = (attribute.Equals( "" ) ? xn.InnerText : xn.Attributes[attribute].Value);
            }
            catch { }
            return value;
        }
 
        /// <summary>
        /// 插入數(shù)據(jù)
        /// </summary>
        /// <param name="path">路徑</param>
        /// <param name="node">節(jié)點(diǎn)</param>
        /// <param name="element">元素名,非空時(shí)插入新元素,,否則在該元素中插入屬性</param>
        /// <param name="attribute">屬性名,非空時(shí)插入該元素屬性值,,否則插入元素值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
         * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
         * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
         ************************************************/
        public static void Insert( string path, string node, string element, string attribute, string value )
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load( path );
                XmlNode xn = doc.SelectSingleNode( node );
                if (element.Equals( "" ))
                {
                    if (!attribute.Equals( "" ))
                    {
                        XmlElement xe = (XmlElement)xn;
                        xe.SetAttribute( attribute, value );
                    }
                }
                else
                {
                    XmlElement xe = doc.CreateElement( element );
                    if (attribute.Equals( "" ))
                        xe.InnerText = value;
                    else
                        xe.SetAttribute( attribute, value );
                    xn.AppendChild( xe );
                }
                doc.Save( path );
            }
            catch { }
        }
 
        /// <summary>
        /// 修改數(shù)據(jù)
        /// </summary>
        /// <param name="path">路徑</param>
        /// <param name="node">節(jié)點(diǎn)</param>
        /// <param name="attribute">屬性名,,非空時(shí)修改該節(jié)點(diǎn)屬性值,否則修改節(jié)點(diǎn)值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Insert(path, "/Node", "", "Value")
         * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
         ************************************************/
        public static void Update( string path, string node, string attribute, string value )
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load( path );
                XmlNode xn = doc.SelectSingleNode( node );
                XmlElement xe = (XmlElement)xn;
                if (attribute.Equals( "" ))
                    xe.InnerText = value;
                else
                    xe.SetAttribute( attribute, value );
                doc.Save( path );
            }
            catch { }
        }
 
        /// <summary>
        /// 刪除數(shù)據(jù)
        /// </summary>
        /// <param name="path">路徑</param>
        /// <param name="node">節(jié)點(diǎn)</param>
        /// <param name="attribute">屬性名,,非空時(shí)刪除該節(jié)點(diǎn)屬性值,,否則刪除節(jié)點(diǎn)值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Delete(path, "/Node", "")
         * XmlHelper.Delete(path, "/Node", "Attribute")
         ************************************************/
        public static void Delete( string path, string node, string attribute )
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load( path );
                XmlNode xn = doc.SelectSingleNode( node );
                XmlElement xe = (XmlElement)xn;
                if (attribute.Equals( "" ))
                    xn.ParentNode.RemoveChild( xn );
                else
                    xe.RemoveAttribute( attribute );
                doc.Save( path );
            }
            catch { }
        }
 
        #region 讀取XML資源到DataSet中
        /// <summary>
        /// 讀取XML資源到DataSet中
        /// </summary>
        /// <param name="source">XML資源,文件為路徑,,否則為XML字符串</param>
        /// <param name="xmlType">XML資源類型</param>
        /// <returns>DataSet</returns>
        public static DataSet GetDataSet( string source, XmlType xmlType )
        {
            DataSet ds = new DataSet();
            if (xmlType == XmlType.File)
            {
                ds.ReadXml( source );
            }
            else
            {
                XmlDocument xd = new XmlDocument();
                xd.LoadXml( source );
                XmlNodeReader xnr = new XmlNodeReader( xd );
                ds.ReadXml( xnr );
            }
            return ds;
        }
 
        #endregion
 
        #region 操作xml文件中指定節(jié)點(diǎn)的數(shù)據(jù)
        /// <summary>
        /// 獲得xml文件中指定節(jié)點(diǎn)的節(jié)點(diǎn)數(shù)據(jù)
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static string GetNodeInfoByNodeName( string path, string nodeName )
        {
            string XmlString = "";
            XmlDocument xml = new XmlDocument();
            xml.Load( path );
            System.Xml.XmlElement root = xml.DocumentElement;
            System.Xml.XmlNode node = root.SelectSingleNode( "http://" + nodeName );
            if (node != null)
            {
                XmlString = node.InnerText;
            }
            return XmlString;
        }
        #endregion
        #region 獲取一個(gè)字符串xml文檔中的ds
        /// <summary>
        /// 獲取一個(gè)字符串xml文檔中的ds
        /// </summary>
        /// <param name="xml_string">含有xml信息的字符串</param>
        public static void get_XmlValue_ds( string xml_string, ref DataSet ds )
        {
            System.Xml.XmlDocument xd = new XmlDocument();
            xd.LoadXml( xml_string );
            XmlNodeReader xnr = new XmlNodeReader( xd );
            ds.ReadXml( xnr );
            xnr.Close();
            int a = ds.Tables.Count;
        }
        #endregion
 
        #region 讀取XML資源到DataTable中
        /// <summary>
        /// 讀取XML資源到DataTable中
        /// </summary>
        /// <param name="source">XML資源,,文件為路徑,否則為XML字符串</param>
        /// <param name="xmlType">XML資源類型:文件,,字符串</param>
        /// <param name="tableName">表名稱</param>
        /// <returns>DataTable</returns>
        public static DataTable GetTable( string source, XmlType xmlType, string tableName )
        {
            DataSet ds = new DataSet();
            if (xmlType == XmlType.File)
            {
                ds.ReadXml( source );
            }
            else
            {
                XmlDocument xd = new XmlDocument();
                xd.LoadXml( source );
                XmlNodeReader xnr = new XmlNodeReader( xd );
                ds.ReadXml( xnr );
            }
            return ds.Tables[tableName];
        }
        #endregion
 
        #region 讀取XML資源中指定的DataTable的指定行指定列的值
        /// <summary>
        /// 讀取XML資源中指定的DataTable的指定行指定列的值
        /// </summary>
        /// <param name="source">XML資源</param>
        /// <param name="xmlType">XML資源類型:文件,,字符串</param>
        /// <param name="tableName">表名</param>
        /// <param name="rowIndex">行號(hào)</param>
        /// <param name="colName">列名</param>
        /// <returns>值,不存在時(shí)返回Null</returns>
        public static object GetTableCell( string source, XmlType xmlType, string tableName, int rowIndex, string colName )
        {
            DataSet ds = new DataSet();
            if (xmlType == XmlType.File)
            {
                ds.ReadXml( source );
            }
            else
            {
                XmlDocument xd = new XmlDocument();
                xd.LoadXml( source );
                XmlNodeReader xnr = new XmlNodeReader( xd );
                ds.ReadXml( xnr );
            }
            return ds.Tables[tableName].Rows[rowIndex][colName];
        }
        #endregion
 
        #region 讀取XML資源中指定的DataTable的指定行指定列的值
        /// <summary>
        /// 讀取XML資源中指定的DataTable的指定行指定列的值
        /// </summary>
        /// <param name="source">XML資源</param>
        /// <param name="xmlType">XML資源類型:文件,,字符串</param>
        /// <param name="tableName">表名</param>
        /// <param name="rowIndex">行號(hào)</param>
        /// <param name="colIndex">列號(hào)</param>
        /// <returns>值,,不存在時(shí)返回Null</returns>
        public static object GetTableCell( string source, XmlType xmlType, string tableName, int rowIndex, int colIndex )
        {
            DataSet ds = new DataSet();
            if (xmlType == XmlType.File)
            {
                ds.ReadXml( source );
            }
            else
            {
                XmlDocument xd = new XmlDocument();
                xd.LoadXml( source );
                XmlNodeReader xnr = new XmlNodeReader( xd );
                ds.ReadXml( xnr );
            }
            return ds.Tables[tableName].Rows[rowIndex][colIndex];
        }
        #endregion
 
        #region 將DataTable寫入XML文件中
        /// <summary>
        /// 將DataTable寫入XML文件中
        /// </summary>
        /// <param name="dt">含有數(shù)據(jù)的DataTable</param>
        /// <param name="filePath">文件路徑</param>
        public static void SaveTableToFile( DataTable dt, string filePath )
        {
            DataSet ds = new DataSet( "Config" );
            ds.Tables.Add( dt.Copy() );
            ds.WriteXml( filePath );
        }
        #endregion
 
        #region 將DataTable以指定的根結(jié)點(diǎn)名稱寫入文件
        /// <summary>
        /// 將DataTable以指定的根結(jié)點(diǎn)名稱寫入文件
        /// </summary>
        /// <param name="dt">含有數(shù)據(jù)的DataTable</param>
        /// <param name="rootName">根結(jié)點(diǎn)名稱</param>
        /// <param name="filePath">文件路徑</param>
        public static void SaveTableToFile( DataTable dt, string rootName, string filePath )
        {
            DataSet ds = new DataSet( rootName );
            ds.Tables.Add( dt.Copy() );
            ds.WriteXml( filePath );
        }
        #endregion
 
        #region 使用DataSet方式更新XML文件節(jié)點(diǎn)
        /// <summary>
        /// 使用DataSet方式更新XML文件節(jié)點(diǎn)
        /// </summary>
        /// <param name="filePath">XML文件路徑</param>
        /// <param name="tableName">表名稱</param>
        /// <param name="rowIndex">行號(hào)</param>
        /// <param name="colName">列名</param>
        /// <param name="content">更新值</param>
        /// <returns>更新是否成功</returns>
        public static bool UpdateTableCell( string filePath, string tableName, int rowIndex, string colName, string content )
        {
            bool flag = false;
            DataSet ds = new DataSet();
            ds.ReadXml( filePath );
            DataTable dt = ds.Tables[tableName];
 
            if (dt.Rows[rowIndex][colName] != null)
            {
                dt.Rows[rowIndex][colName] = content;
                ds.WriteXml( filePath );
                flag = true;
            }
            else
            {
                flag = false;
            }
            return flag;
        }
        #endregion
 
        #region 使用DataSet方式更新XML文件節(jié)點(diǎn)
        /// <summary>
        /// 使用DataSet方式更新XML文件節(jié)點(diǎn)
        /// </summary>
        /// <param name="filePath">XML文件路徑</param>
        /// <param name="tableName">表名稱</param>
        /// <param name="rowIndex">行號(hào)</param>
        /// <param name="colIndex">列號(hào)</param>
        /// <param name="content">更新值</param>
        /// <returns>更新是否成功</returns>
        public static bool UpdateTableCell( string filePath, string tableName, int rowIndex, int colIndex, string content )
        {
            bool flag = false;
 
            DataSet ds = new DataSet();
            ds.ReadXml( filePath );
            DataTable dt = ds.Tables[tableName];
 
            if (dt.Rows[rowIndex][colIndex] != null)
            {
                dt.Rows[rowIndex][colIndex] = content;
                ds.WriteXml( filePath );
                flag = true;
            }
            else
            {
                flag = false;
            }
            return flag;
        }
        #endregion
 
        #region 讀取XML資源中的指定節(jié)點(diǎn)內(nèi)容
        /// <summary>
        /// 讀取XML資源中的指定節(jié)點(diǎn)內(nèi)容
        /// </summary>
        /// <param name="source">XML資源</param>
        /// <param name="xmlType">XML資源類型:文件,字符串</param>
        /// <param name="nodeName">節(jié)點(diǎn)名稱</param>
        /// <returns>節(jié)點(diǎn)內(nèi)容</returns>
        public static object GetNodeValue( string source, XmlType xmlType, string nodeName )
        {
            XmlDocument xd = new XmlDocument();
            if (xmlType == XmlType.File)
            {
                xd.Load( source );
            }
            else
            {
                xd.LoadXml( source );
            }
            XmlElement xe = xd.DocumentElement;
            XmlNode xn = xe.SelectSingleNode( "http://" + nodeName );
            if (xn != null)
            {
                return xn.InnerText;
            }
            else
            {
                return null;
            }
        }
 
        /// <summary>
        /// 讀取XML資源中的指定節(jié)點(diǎn)內(nèi)容
        /// </summary>
        /// <param name="source">XML資源</param>
        /// <param name="nodeName">節(jié)點(diǎn)名稱</param>
        /// <returns>節(jié)點(diǎn)內(nèi)容</returns>
        public static object GetNodeValue( string source, string nodeName )
        {
            if (source == null || nodeName == null || source == "" || nodeName == "" || source.Length < nodeName.Length * 2)
            {
                return null;
            }
            else
            {
                int start = source.IndexOf( "<" + nodeName + ">" ) + nodeName.Length + 2;
                int end = source.IndexOf( "</" + nodeName + ">" );
                if (start == -1 || end == -1)
                {
                    return null;
                }
                else if (start >= end)
                {
                    return null;
                }
                else
                {
                    return source.Substring( start, end - start );
                }
            }
        }
        #endregion
 
        #region 更新XML文件中的指定節(jié)點(diǎn)內(nèi)容
        /// <summary>
        /// 更新XML文件中的指定節(jié)點(diǎn)內(nèi)容
        /// </summary>
        /// <param name="filePath">文件路徑</param>
        /// <param name="nodeName">節(jié)點(diǎn)名稱</param>
        /// <param name="nodeValue">更新內(nèi)容</param>
        /// <returns>更新是否成功</returns>
        public static bool UpdateNode( string filePath, string nodeName, string nodeValue )
        {
            bool flag = false;
 
            XmlDocument xd = new XmlDocument();
            xd.Load( filePath );
            XmlElement xe = xd.DocumentElement;
            XmlNode xn = xe.SelectSingleNode( "http://" + nodeName );
            if (xn != null)
            {
                xn.InnerText = nodeValue;
                flag = true;
            }
            else
            {
                flag = false;
            }
            return flag;
        }
        #endregion
 
 
        /// <summary>
        /// 讀取xml文件,,并將文件序列化為類
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T ReadXML<T>( string path )
        {
            XmlSerializer reader = new XmlSerializer( typeof( JJDB ) );
            StreamReader file = new StreamReader( @path );
            return (T)reader.Deserialize( file );
        }
 
        /// <summary>
        /// 將對(duì)象寫入XML文件
        /// </summary>
        /// <typeparam name="T">C#對(duì)象名</typeparam>
        /// <param name="item">對(duì)象實(shí)例</param>
        /// <param name="path">路徑</param>
        /// <param name="jjdbh">標(biāo)號(hào)</param>
        /// <param name="ends">結(jié)束符號(hào)(整個(gè)xml的路徑類似如下:C:\xmltest\201111send.xml,,其中path=C:\xmltest,jjdbh=201111,ends=send)</param>
        /// <returns></returns>
        public static string WriteXML<T>( T item, string path, string jjdbh, string ends )
        {
            if (string.IsNullOrEmpty( ends ))
            {
                //默認(rèn)為發(fā)送
                ends = "send";
            }
            int i = 0;//控制寫入文件的次數(shù),
            XmlSerializer serializer = new XmlSerializer( item.GetType() );
            object[] obj = new object[] { path, "\\", jjdbh, ends, ".xml" };
            string xmlPath = String.Concat( obj );
            while (true)
            {
                try
                {
                    //用filestream方式創(chuàng)建文件不會(huì)出現(xiàn)“文件正在占用中,,用File.create”則不行
                    FileStream fs;
                    fs = File.Create( xmlPath );
                    fs.Close();
                    TextWriter writer = new StreamWriter( xmlPath, false, Encoding.UTF8 );
                    XmlSerializerNamespaces xml = new XmlSerializerNamespaces();
                    xml.Add( string.Empty, string.Empty );
                    serializer.Serialize( writer, item, xml );
                    writer.Flush();
                    writer.Close();
                    break;
                }
                catch (Exception exec)
                {
                    if (i < 5)
                    {
                        i++;
                        continue;
                    }
                    else
                    { break; }
                }
            }
            return SerializeToXmlStr<T>( item, true );
        }
 
        /// <summary>
        /// 靜態(tài)擴(kuò)展
        /// </summary>
        /// <typeparam name="T">需要序列化的對(duì)象類型,,必須聲明[Serializable]特征</typeparam>
        /// <param name="obj">需要序列化的對(duì)象</param>
        /// <param name="omitXmlDeclaration">true:省略XML聲明;否則為false.默認(rèn)false,即編寫 XML 聲明,。</param>
        /// <returns></returns>
        public static string SerializeToXmlStr<T>( T obj, bool omitXmlDeclaration )
        {
 
            return XmlSerialize<T>( obj, omitXmlDeclaration );
        }
        #region XML序列化反序列化相關(guān)的靜態(tài)方法
        /// <summary>
        /// 使用XmlSerializer序列化對(duì)象
        /// </summary>
        /// <typeparam name="T">需要序列化的對(duì)象類型,,必須聲明[Serializable]特征</typeparam>
        /// <param name="obj">需要序列化的對(duì)象</param>
        /// <param name="omitXmlDeclaration">true:省略XML聲明;否則為false.默認(rèn)false,即編寫 XML 聲明,。</param>
        /// <returns>序列化后的字符串</returns>
        public static string XmlSerialize<T>( T obj, bool omitXmlDeclaration )
        {
 
            /* This property only applies to XmlWriter instances that output text content to a stream; otherwise, this setting is ignored.
            可能很多朋友遇見過 不能轉(zhuǎn)換成Xml不能反序列化成為UTF8XML聲明的情況,,就是這個(gè)原因。
            */
            XmlWriterSettings xmlSettings = new XmlWriterSettings();
            xmlSettings.OmitXmlDeclaration = omitXmlDeclaration;
            xmlSettings.Encoding = new System.Text.UTF8Encoding( false );
            MemoryStream stream = new MemoryStream();//var writer = new StringWriter();
            XmlWriter xmlwriter = XmlWriter.Create( stream/*writer*/, xmlSettings ); //這里如果直接寫成:Encoding = Encoding.UTF8 會(huì)在生成的xml中加入BOM(Byte-order Mark) 信息(Unicode 字節(jié)順序標(biāo)記) ,, 所以new System.Text.UTF8Encoding(false)是最佳方式,,省得再做替換的麻煩
            XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
            xmlns.Add( String.Empty, String.Empty ); //在XML序列化時(shí)去除默認(rèn)命名空間xmlns:xsd和xmlns:xsi
            XmlSerializer ser = new XmlSerializer( typeof( T ) );
            ser.Serialize( xmlwriter, obj, xmlns );
 
            return Encoding.UTF8.GetString( stream.ToArray() );//writer.ToString();
        }
 
        /// <summary>
        /// 使用XmlSerializer序列化對(duì)象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">文件路徑</param>
        /// <param name="obj">需要序列化的對(duì)象</param>
        /// <param name="omitXmlDeclaration">true:省略XML聲明;否則為false.默認(rèn)false,即編寫 XML 聲明,。</param>
        /// <param name="removeDefaultNamespace">是否移除默認(rèn)名稱空間(如果對(duì)象定義時(shí)指定了:XmlRoot(Namespace = "http://www./xsd")則需要傳false值進(jìn)來)</param>
        /// <returns>序列化后的字符串</returns>
        public static void XmlSerialize<T>( string path, T obj, bool omitXmlDeclaration, bool removeDefaultNamespace )
        {
            XmlWriterSettings xmlSetings = new XmlWriterSettings();
            xmlSetings.OmitXmlDeclaration = omitXmlDeclaration;
            using (XmlWriter xmlwriter = XmlWriter.Create( path, xmlSetings ))
            {
                XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
                if (removeDefaultNamespace)
                    xmlns.Add( String.Empty, String.Empty ); //在XML序列化時(shí)去除默認(rèn)命名空間xmlns:xsd和xmlns:xsi
                XmlSerializer ser = new XmlSerializer( typeof( T ) );
                ser.Serialize( xmlwriter, obj, xmlns );
            }
        }
 
        private static byte[] ShareReadFile( string filePath )
        {
            byte[] bytes;
            //避免"正由另一進(jìn)程使用,因此該進(jìn)程無法訪問此文件"造成異常 共享鎖 flieShare必須為ReadWrite,,但是如果文件不存在的話,還是會(huì)出現(xiàn)異常,,所以這里不能吃掉任何異常,,但是需要考慮到這些問題
            using (FileStream fs = new FileStream( filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ))
            {
                bytes = new byte[fs.Length];
                int numBytesToRead = (int)fs.Length;
                int numBytesRead = 0;
                while (numBytesToRead > 0)
                {
                    int n = fs.Read( bytes, numBytesRead, numBytesToRead );
                    if (n == 0)
                        break;
                    numBytesRead += n;
                    numBytesToRead -= n;
                }
            }
            return bytes;
        }
        /// <summary>
        /// 從文件讀取并反序列化為對(duì)象 (解決: 多線程或多進(jìn)程下讀寫并發(fā)問題)
        /// </summary>
        /// <typeparam name="T">返回的對(duì)象類型</typeparam>
        /// <param name="path">文件地址</param>
        /// <returns></returns>
        public static T XmlFileDeserialize<T>( string path )
        {
            byte[] bytes = ShareReadFile( path );
            if (bytes.Length < 1)//當(dāng)文件正在被寫入數(shù)據(jù)時(shí),,可能讀出為0
                for (int i = 0; i < 5; i++)
                { //5次機(jī)會(huì)
                    bytes = ShareReadFile( path ); // 采用這樣詭異的做法避免獨(dú)占文件和文件正在被寫入時(shí)讀出來的數(shù)據(jù)為0字節(jié)的問題。
                    if (bytes.Length > 0) break;
                    System.Threading.Thread.Sleep( 50 ); //悲觀情況下總共最多消耗1/4秒,,讀取文件
                }
            XmlDocument doc = new XmlDocument();
            doc.Load( new MemoryStream( bytes ) );
            if (doc.DocumentElement != null)
                return (T)new XmlSerializer( typeof( T ) ).Deserialize( new XmlNodeReader( doc.DocumentElement ) );
            return default( T );
            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
            xmlReaderSettings.CloseInput = true;
            using (XmlReader xmlReader = XmlReader.Create( path, xmlReaderSettings ))
            {
                T obj = (T)new XmlSerializer( typeof( T ) ).Deserialize( xmlReader );
                return obj;
            }
        }
 
        /// <summary>
        /// 使用XmlSerializer反序列化對(duì)象
        /// </summary>
        /// <param name="xmlOfObject">需要反序列化的xml字符串</param>
        /// <returns>反序列化后的對(duì)象</returns>
        public static T XmlDeserialize<T>( string xmlOfObject ) where T : class
        {
            XmlReader xmlReader = XmlReader.Create( new StringReader( xmlOfObject ), new XmlReaderSettings() );
            return (T)new XmlSerializer( typeof( T ) ).Deserialize( xmlReader );
        }
 
        #endregion
    }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多