首页 > 代码库 > Xml 序列化 Json 反序列化

Xml 序列化 Json 反序列化

 #region 全局变量        /// <summary>        /// 全局变量        /// </summary>        XmlDocument XmlDoc = new XmlDocument();        #endregion        #region 加载        public ParseJosn()        {            InitializeComponent();            ParseJson(XmlPaerseJson());        }         #endregion        #region     Json 转换Model               /// <summary>        /// json转换到model        /// </summary>        /// <param name="list">Json 字符数组</param>        private void ParseJson(List<string> list)          {            List<JsonModel> ListModel = new List<JsonModel>();            foreach (var item in list)            {                var Json = (JObject)JsonConvert.DeserializeObject(item);                JsonModel JModel = new JsonModel();                JModel.UserId = int.Parse(Json["UserTable"]["UserID"].ToString());                JModel.UserName = Json["UserTable"]["UserName"].ToString();                JModel.UserAge = int.Parse(Json["UserTable"]["UserAge"].ToString());                ListModel.Add(JModel);            }            UserTableJson.DataContext = ListModel;        }        #endregion        #region  Xml转换成Json        private List<string> XmlPaerseJson()        {            XmlDoc.Load(@"../../XML/UserAddDelectUpdate.xml");            XmlNode XNode = XmlDoc.DocumentElement;            List<string> list = new List<string>();            XmlNodeList nodes = XNode.ChildNodes;            foreach (XmlNode item in nodes)            {                string JosnText = Newtonsoft.Json.JsonConvert.SerializeObject(item);                list.Add(JosnText);            }            return list;        }        #endregion