将XmlDocument转化String,将String转化为 XmlDocument

 /// <summary>  
        /// 将XmlDocument转化为string  
        /// </summary>  
        /// <param name="xmlDoc">XML文件</param>  
        /// <returns></returns>  
        public string ConvertXmlToString(XmlDocument xmlDoc)
        {
            MemoryStream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, null);
            writer.Formatting = Formatting.Indented;
            xmlDoc.Save(writer);
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
            stream.Position = 0;
            string xmlString = sr.ReadToEnd();
            sr.Close();
            stream.Close();
            return xmlString;
        }

        /// <summary>  
        /// 将string转化为 XmlDocument
        /// </summary>  
        /// <param name="str">XML文件</param>  
        /// <param name="filepath">XML文件路径</param>  
        /// <returns></returns>  
        public string ConvertStringToXml(string str, string filepath)
        {
            string result = string.Empty;
            XmlDocument dom = new XmlDocument();
            dom.LoadXml(str);
            dom.Save(filepath);
            bool m_isFileExisted = File.Exists(filepath);
            if (m_isFileExisted)
                result = "success";
            return result;
        }

 

 

 

posted @ 2016-06-28 09:18  Akon.Ray  阅读(2645)  评论(0编辑  收藏  举报