c# 将xml解析成一个类,和解析tomcat配置文件示例

将xml解析成一个类,方便查询使用。
类:

 public class XMLObject : BaseGuid
    {
        public string NodeName { get; set; }
        public Dictionary<string, string> Attributes { get; set; }

        public string FartherName { get; set; }

        public string FartherID { get; set; }

        public List<XMLObject> Childs
        {
            get
            {
                return _childs;
            }

            set
            {
                _childs = value;
            }
        }

        private List<XMLObject> _childs = new List<XMLObject>();

        public static XMLObject GetXmlObj(XmlDocument doc, XmlElement farther, XMLObject xmlobj = null, XMLObject fartherObj = null)
        {
            XmlElement el;
            if (xmlobj == null)
            {
                el = doc.DocumentElement;

                xmlobj = new XMLObject();
                xmlobj.NodeName = el.Name;
                xmlobj.Attributes = getElAttri(el);
            }
            else
            {
                el = farther;
            }

            foreach (XmlNode node in el.ChildNodes)
            {
                if (node.NodeType != XmlNodeType.Comment)
                {
                    XMLObject obj = new XMLObject();
                    obj.FartherName = el.Name;
                    obj.NodeName = node.Name;
                    obj.Attributes = getElAttri(node as XmlElement);

                    if (fartherObj != null)
                    {
                        obj.FartherID = fartherObj.ID;
                        fartherObj.Childs.Add(obj);
                    }
                    else
                    {
                        obj.FartherID = xmlobj.ID;
                        xmlobj.Childs.Add(obj);
                    }

                    if (node.ChildNodes.Count > 0)
                    {
                        GetXmlObj(doc, node as XmlElement, xmlobj, obj);
                    }
                }
            }
            return xmlobj;
        }

        private static Dictionary<string, string> getElAttri(XmlElement el)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (XmlAttribute attr in el.Attributes)
            {
                dic.Add(attr.Name, attr.Value);
            }
            return dic;
        }
    }

tomcat配置文件conf.xml解析示例:

 public List<WebSite> ListSite()
        {
            string conf = _tomcatDir + "\\conf\\server.xml";
            if (!File.Exists(conf))
            {
                throw new Exception(string.Format("未找到tomcat的配置文件:{0}", conf));
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(conf);
            XmlElement root = doc.DocumentElement;

            XMLObject xmlobj = XMLObject.GetXmlObj(doc, root);

            List<XMLObject> listWeb = xmlobj.Childs.Where(f => f.NodeName.ToLower() == "service").ToList();

            List<WebSite> list = new List<WebSite>();
            foreach (var obj in listWeb)
            {
                WebSite site = new WebSite();
                site.SiteName = obj.Attributes.FirstOrDefault(f => f.Key.ToLower() == "name").Value;
                var connector = obj.Childs.SingleOrDefault(f => f.NodeName.ToLower() == "connector" && f.Attributes["protocol"].ToLower().Contains("http"));

                var engine = obj.Childs.SingleOrDefault(f => f.NodeName.ToLower() == "engine");
                var host = engine.Childs.Where(f => f.NodeName.ToLower() == "host");
                foreach (var v in host)
                {
                    WebSite.Bind bind = new WebSite.Bind();
                    bind.Port = int.Parse(connector.Attributes.FirstOrDefault(f => f.Key.ToLower() == "port").Value);
                    bind.Host = v.Attributes.FirstOrDefault(f => f.Key.ToLower() == "name").Value; ;

                    site.BindList.Add(bind);

                    var context = v.Childs.SingleOrDefault(f => f.NodeName.ToLower() == "context");
                    if (context != null)
                    {
                        site.PhysicalPath = context.Attributes.FirstOrDefault(f => f.Key.ToLower() == "docbase").Value;
                    }
                }
                list.Add(site);
            }
            return list;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值