예제 #1
0
        /// <summary>
        /// 保存对象到特定格式的XML文件
        /// </summary>
        /// <param name="path">XML文件路径.</param>
        /// <param name="obj">待保存的对象</param>
        public static void SaveObjectToXml(string path, object obj)
        {
            string xml = XmlConvertor.ObjectToXml(obj, true);

            using (StreamWriter writer = new StreamWriter(path))
            {
                writer.Write(xml);
            }
        }
예제 #2
0
        /// <summary>
        /// 从XML文件转换为Object对象类型.
        /// </summary>
        /// <param name="path">XML文件路径</param>
        /// <param name="type">Object对象类型</param>
        /// <returns></returns>
        public static object LoadObjectFromXml(string path, Type type)
        {
            object obj = null;

            using (StreamReader reader = new StreamReader(path))
            {
                string content = reader.ReadToEnd();
                obj = XmlConvertor.XmlToObject(content, type);
            }
            return(obj);
        }
예제 #3
0
 /// <summary>
 /// 转换XML字符串到指定类型的对象
 /// </summary>
 /// <typeparam name="T">指定的对象类型</typeparam>
 /// <param name="xml">XML字符串</param>
 /// <returns>从XML字符串转换过来的对象</returns>
 public static T XmlToObject <T>(string xml) where T : class
 {
     return(XmlConvertor.XmlToObject(xml, typeof(T)) as T);
 }