Exemplo n.º 1
0
        /// <summary>
        /// 以实体类为单位进行保存
        /// 对象属性为Null的,不保存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public int Save <T>(T obj, string nodeName = null)
        {
            int  result = 0;
            Type type   = typeof(T);

            PropertyInfo[] propers = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            if (nodeName == null)
            {
                foreach (var m in type.GetCustomAttributes(true))
                {
                    if (m is ADSXmlNode)
                    {
                        ADSXmlNode node = (ADSXmlNode)m;
                        nodeName = node.NodeName;
                    }
                }
            }
            if (nodeName == null)
            {
                throw new Exception("未指定类的节点特性");
            }
            //XmlNode xmlNode= objXmlDoc.SelectSingleNode("Root/Basic/"+nodeName);
            findNode(objXmlDoc, nodeName);
            if (!NodeCurrent.Name.Equals(nodeName))
            {
                return(0);
            }
            foreach (PropertyInfo p in propers)
            {
                var value = p.GetValue(obj, null);
                if (value != null)
                {
                    NodeCurrent.SelectSingleNode(p.Name).InnerText = value.ToString();
                    result++;
                }
            }
            objXmlDoc.Save(strXmlFile);
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取Xml结果
        /// </summary>
        /// <typeparam name="TResult">Model</typeparam>
        /// <param name="nodeName">可指定节点名称</param>
        /// <returns></returns>
        public TResult Get <TResult>(string nodeName = null)
        {
            getData();
            Type    type = typeof(TResult);
            TResult obj  = (TResult)Activator.CreateInstance(type);

            PropertyInfo[] propers = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            if (nodeName == null)
            {
                foreach (var m in type.GetCustomAttributes(true))
                {
                    if (m is ADSXmlNode)
                    {
                        ADSXmlNode node = (ADSXmlNode)m;
                        nodeName = node.NodeName;
                    }
                }
            }
            if (nodeName == null)
            {
                throw new Exception("未指定类的节点特性");
            }
            //Dictionary<string, object> dic = (Dictionary<string, object>)XmlContent[nodeName];
            findDictionary(XmlContent, nodeName);
            if (!DictionaryCurrent.ContainsKey(nodeName))
            {
                throw new Exception("Xml文件不存在指定节点");
            }
            Dictionary <string, object> dic = (Dictionary <string, object>)DictionaryCurrent[nodeName];

            foreach (PropertyInfo p in propers)
            {
                if (dic.ContainsKey(p.Name))
                {
                    p.SetValue(obj, dic[p.Name], null);
                }
            }
            return(obj);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 保存单个叶子节点
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp"></param>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public bool SaveSingle <T>(Expression <Func <T, T> > exp, string nodeName = null)
        {
            Type type = typeof(T);

            if (nodeName == null)
            {
                foreach (var m in type.GetCustomAttributes(true))
                {
                    if (m is ADSXmlNode)
                    {
                        ADSXmlNode node = (ADSXmlNode)m;
                        nodeName = node.NodeName;
                    }
                }
            }
            if (nodeName == null)
            {
                throw new Exception("未指定类的节点特性");
            }
            //XmlNode xmlNode = objXmlDoc.SelectSingleNode("Root/Basic/" + nodeName);
            findNode(objXmlDoc, nodeName);
            if (!NodeCurrent.Name.Equals(nodeName))
            {
                return(false);
            }

            MemberInitExpression body = (MemberInitExpression)exp.Body;

            foreach (MemberAssignment item in body.Bindings)
            {
                var name = item.Member.Name;
                ConstantExpression single = (ConstantExpression)item.Expression;
                var value = single.Value;
                NodeCurrent.SelectSingleNode(name).InnerText = value.ToString();
            }
            objXmlDoc.Save(strXmlFile);
            return(true);
        }