Beispiel #1
0
        public static void GetNodeAttribute(this string xmlFile, XmlNodeDataAttribute parentNode, ChildAttributeInNodeEvent attEvent)
        {
            List <XmlNodeDataAttribute> atts = new List <XmlNodeDataAttribute>();
            XmlDocument doc = new XmlDocument();

            doc.Load(xmlFile);
            string  nodeFormat = "[@{0}='{1}']";
            XmlNode node       = doc.SelectSingleNode(parentNode.NodeName + string.Format(nodeFormat, parentNode.NodeKeyName, parentNode.NodeKeyValue));

            if (node == null)
            {//未找到节点
                return;
            }
            XmlNodeList targets = node.ChildNodes;//全部子节点

            foreach (XmlNode item in targets)
            {
                if (item.NodeType == XmlNodeType.Comment || item.NodeType == XmlNodeType.Text)
                {
                    continue;
                }
                XmlElement ele = (XmlElement)item;
                attEvent(ele.Attributes);
            }
        }
Beispiel #2
0
        public static T GetNodeSpecialeAttribute <T>(this string xmlFile, XmlNodeDataAttribute parentNode, XmlNodeDataAttribute nodeAttribute) where T : class
        {
            string[] att  = new string[] { nodeAttribute.NodeKeyName, nodeAttribute.NodeKeyValue };
            T        data = System.Activator.CreateInstance <T>();
            Type     objT = typeof(T);

            GetNodeAttribute(xmlFile, parentNode, new ChildAttributeInNodeEvent(atts =>
            { //读取每一项(属性)的值
                XmlAttribute obj = atts[att[0]];
                if (obj == null)
                {
                    return;
                }
                XmlAttribute recValue = atts[att[1]];
                if (recValue == null || string.IsNullOrEmpty(recValue.Value))
                {
                    return;
                }
                string property = obj.Value;
                PropertyInfo pi = objT.GetProperty(property);
                if (pi == null)
                {
                    return;
                }
                string value = recValue.Value.ToLower().Trim();
                if (pi.PropertyType.Name == typeof(bool).Name)
                { //xml中节点存储bool类型的内容不能直接转换为bool
                    // 存的数据可能情形 0/1 ,false/true
                    string[] arr = new string[] { "false", "true" };

                    if (arr.Contains(value))
                    { //数值
                        pi.SetValue(data, (value != "true" ? true : false), null);
                        return;
                    }
                    else
                    {
                        pi.SetValue(data, (value == "1" ? true : false), null);
                        return;
                    }
                }

                pi.SetValue(data, Convert.ChangeType(value, pi.PropertyType), null);
            }));
            return(data);
        }
Beispiel #3
0
        public static void UpdateXmlNode <T>(this string xmlFile, T data, XmlNodeDataAttribute parentNode, XmlNodeDataAttribute nodeAttribute) where T : class
        {
            string[] xmlAtt = new string[] { nodeAttribute.NodeKeyName, nodeAttribute.NodeKeyValue };
            Type     obj    = typeof(T);

            PropertyInfo[] pis = obj.GetProperties();
            XmlDocument    doc = new XmlDocument();

            doc.Load(xmlFile);
            string  nodeFormat = "[@{0}='{1}']";
            string  appItem    = string.Format(nodeFormat, parentNode.NodeKeyName, parentNode.NodeKeyValue);
            XmlNode node       = doc.SelectSingleNode(parentNode.NodeName + appItem);

            //读取xml
            foreach (PropertyInfo item in pis)
            {
                //首先查看特性是否存储
                string  rec         = item.Name;
                object  appValue    = item.GetValue(data, null);
                string  appValueStr = appValue == null ? string.Empty : appValue.ToString();
                XmlNode row         = node.SelectSingleNode(nodeAttribute.NodeName + string.Format(nodeFormat, nodeAttribute.NodeKeyName, rec));//是否有这一属性
                if (row == null)
                {
                    XmlNode    xm  = doc.CreateNode("element", nodeAttribute.NodeName, string.Empty);
                    XmlElement ele = (XmlElement)xm;
                    ele.SetAttribute(nodeAttribute.NodeKeyName, rec);
                    ele.SetAttribute(nodeAttribute.NodeKeyValue, appValueStr);
                    node.AppendChild(xm);
                }
                else
                {//更改这一行配置的值
                    XmlNode    newNode = row.CloneNode(true);
                    XmlElement ele     = (XmlElement)newNode;
                    ele.SetAttribute(nodeAttribute.NodeKeyName, rec);
                    ele.SetAttribute(nodeAttribute.NodeKeyValue, appValueStr);
                    node.ReplaceChild(newNode, row);//节点更新
                }
            }
            doc.Save(xmlFile);
        }
Beispiel #4
0
        /// <summary>
        /// 获取节点指定关键字
        /// </summary>
        /// <param name="xmlFile"></param>
        /// <param name="parentNode">节点所属父项</param>
        /// <param name="nodeAttribute">节点项特性内容</param>
        /// <returns></returns>
        public static Dictionary <string, string> GetNodeSpecialeAttribute(this string xmlFile, XmlNodeDataAttribute parentNode, XmlNodeDataAttribute nodeAttribute)
        {
            string[] att = new string[] { nodeAttribute.NodeKeyName, nodeAttribute.NodeKeyValue };
            Dictionary <string, string> cfg = new Dictionary <string, string>();

            GetNodeAttribute(xmlFile, parentNode, new ChildAttributeInNodeEvent(atts =>
            {
                XmlAttribute rec = atts[att[0]];
                if (rec == null)
                {
                    return;
                }
                XmlAttribute recValue = atts[att[1]];
                if (!string.IsNullOrEmpty(rec.Value) && !cfg.ContainsKey(rec.Value))
                {
                    cfg.Add(rec.Value, recValue == null?string.Empty:recValue.Value);
                }
            }));
            return(cfg);
        }
Beispiel #5
0
        public static void UpdateConfigNode(this string xmlFile, Dictionary <string, string> obj, XmlNodeDataAttribute entityBelongNode, XmlNodeDataAttribute dictBelongNode)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(xmlFile);
            //查找目标节点
            XmlNode parent = doc.SelectSingleNode(entityBelongNode.NodeName + "[@" + entityBelongNode.NodeKeyName + "='" + entityBelongNode.NodeKeyValue + "']");
            //查找目标
            string nodeFormat = "[@{0}='{1}']";

            foreach (var item in obj)
            {
                XmlNode node = parent.SelectSingleNode(dictBelongNode.NodeName + string.Format(nodeFormat, dictBelongNode.NodeKeyName, item.Key));
                if (node == null)
                { //如果没有这个节点则需要增加
                    XmlNode    xm  = doc.CreateNode("element", dictBelongNode.NodeName, string.Empty);
                    XmlElement ele = (XmlElement)xm;
                    ele.SetAttribute(dictBelongNode.NodeKeyName, item.Key);
                    ele.SetAttribute(dictBelongNode.NodeKeyValue, item.Value);
                    parent.AppendChild(xm);
                }
                else
                {
                    XmlNode    newNode = node.CloneNode(true);
                    XmlElement ele     = (XmlElement)newNode;
                    ele.SetAttribute(dictBelongNode.NodeKeyName, item.Key);
                    ele.SetAttribute(dictBelongNode.NodeKeyValue, item.Value);
                    parent.ReplaceChild(newNode, node);//节点更新
                }
            }
            doc.Save(xmlFile);
        }