VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之【C#】【对象转XML】xml序列化

笔记:xml序列化


        /// <summary>
        /// xml序列化
        /// </summary>
        /// <param name="root"></param>
        /// <param name="dic"></param>
        /// <returns></returns>

 View Code

 

序列化使用示例

 Dictionary<string, object> apigDic = new Dictionary<string, object>();

 Dictionary<string, object> paramDic= new Dictionary<string, object>();

 Dictionary<string, object> transDic= new Dictionary<string, object>();

apigDic.Add("INFO", XmlSerialize(paramDic));
apigDic.Add("TRANS", XmlSerialize(transDic));
string xmlStr = XmlSerialize("APIG",apigDic).ToString();

 

反序列化:

复制代码
private static Dictionary<string, object> XmlDeSerialize(string xml)
        {
            XElement rootElement;
            try
            {
                 rootElement = XElement.Parse(xml);
            }
            catch (Exception)
            {
                return null;
            }

            Dictionary<string, object> dict = new Dictionary<string, object>();
            foreach (var el in rootElement.Elements())
            {
                
                var el2=XmlDeSerialize(el.ToString());
                if (el2 == null || el2.Count==0)
                    dict.Add(el.Name.LocalName, el.Value);
                else
                    dict.Add(el.Name.LocalName, el2);
            }
            return dict;
        }
复制代码


相关教程