Exemple #1
0
        /// <summary>
        /// 获取一个节点
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public XmlChildNode GetNode(string element)
        {
            if (doc.ChildNodes.Count == 0) return null;

            //如果只有2个节点,说明是要节点
            if (doc.ChildNodes.Count == 2)
            {
                return new XmlChildNode(doc, (XmlNode)doc.DocumentElement);
            }

            string[] elements = element.Split(new char[] { '.', '/', '|' });
            if (elements.Length == 1)
            {
                var node = doc.SelectSingleNode(elements[0]);
                if (node == null) return null;

                return new XmlChildNode(doc, node);
            }

            XmlChildNode help = null;
            foreach (string el in elements)
            {
                var node = doc.SelectSingleNode(elements[0]);
                if (node == null) return null;

                if (help == null)
                    help = new XmlChildNode(doc, node);
                else
                    help = help.GetNode(el);
            }

            return help;
        }
Exemple #2
0
        /// <summary>
        /// 通过属性的值获取节点
        /// </summary>
        /// <param name="attributes"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public XmlChildNode[] GetNodesByAttributeValue(string[] attributes, string[] values)
        {
            var list = new List<XmlChildNode>();

            foreach (XmlNode nd in node.ChildNodes)
            {
                int index = 0;
                int count = 0;
                foreach (string attribute in attributes)
                {
                    if (nd.Attributes[attribute].Value.ToLower() == values[index].ToLower())
                    {
                        count++;
                    }
                    index++;
                }

                if (count == attributes.Length)
                {
                    var helper = new XmlChildNode(doc, nd);
                    list.Add(helper);
                }
            }

            return list.ToArray();
        }
Exemple #3
0
        /// <summary>
        /// 获取节点列表
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public XmlChildNode[] GetNodes(string element)
        {
            var list = new List<XmlChildNode>();
            foreach (XmlNode nd in node.ChildNodes)
            {
                if (nd.Name == element)
                {
                    var helper = new XmlChildNode(doc, nd);
                    list.Add(helper);
                }
            }

            return list.ToArray();
        }