Example #1
0
        private static NameObjectCollection ParseXML(XmlNode node)
        {
            NameObjectCollection col = null;

            if (node != null)
            {
                col = new NameObjectCollection();
                if (node.HasChildNodes)
                {
                    foreach (XmlNode n in node.ChildNodes)
                    {
                        bool hasAttr = false;
                        if (n.Attributes != null && n.Attributes.Count > 0)
                        {
                            hasAttr = true;
                            NameObjectCollection attrCol = new NameObjectCollection();
                            foreach (XmlAttribute a in n.Attributes)
                            {
                                attrCol.Add(a.Name, a.InnerText);
                            }
                            col.Add(n.Name, attrCol);
                        }
                        if (n.NodeType == XmlNodeType.Element && n.ChildNodes.Count == 0)
                        {
                            if (!hasAttr || !string.IsNullOrEmpty(n.InnerText))
                            {
                                col.Add(n.Name, string.Format("{0}", n.InnerText));
                            }
                        }
                        else if (n.FirstChild == n.LastChild &&
                                 (n.FirstChild.NodeType == XmlNodeType.Text ||
                                  n.FirstChild.NodeType == XmlNodeType.CDATA))
                        {
                            col.Add(n.Name, n.InnerText);
                        }
                        else
                        {
                            col.Add(n.Name, ParseXML(n));
                        }
                    }
                }
                else
                {
                    col.Add(node.Name, node.InnerText);
                }
            }
            return(col);
        }
Example #2
0
        /// <summary>
        /// 解析XML文档,不支持属性
        /// </summary>
        /// <param name="xmlContent">XML文档</param>
        /// <returns>字典</returns>
        public static NameObjectCollection ParseXML(string xmlContent)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xmlContent);
            NameObjectCollection col = new NameObjectCollection();

            foreach (XmlNode n in xmlDoc.ChildNodes)
            {
                col.Add(n.Name, ParseXML(n));
            }
            return(col);
        }
Example #3
0
        /// <summary>
        /// 解析XML文档,不支持属性
        /// </summary>
        /// <param name="xmlContent">XML文档</param>
        /// <param name="rootNodeName">要解析的节点名称</param>
        /// <returns>字典</returns>
        public static NameObjectCollection ParseXML(string xmlContent, string rootNodeName)
        {
            NameObjectCollection col = null;

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xmlContent);
                XmlNode node = xmlDoc.SelectSingleNode(rootNodeName);
                col = new NameObjectCollection();
                col.Add(rootNodeName, ParseXML(node));
            }
            catch
            {
            }
            return(col);
        }