Пример #1
0
            public static ConfTree Generate(string xmlPath)
            {
                //_log.Info($"Generate({xmlPath})");

                try
                {
                    var xmlDoc = new XmlDoc();
                    xmlDoc.Load(xmlPath);

                    var node   = xmlDoc.ChildNodes[xmlDoc.ChildNodes.Count - 1];
                    var result = XmlConf.ToTree(node, xmlDoc);
                    if (string.IsNullOrEmpty(result.Tag))
                    {
                        result.Tag = Path.GetFileNameWithoutExtension(xmlPath);
                        XmlConf.AddTag(xmlDoc, node, result.Tag);
                    }
                    result.Source = Source.Xml;
                    result.XmlDoc = xmlDoc;

                    return(result);
                }
                catch (Exception ex)
                {
                    _log.Warn($"Failed to BuildConfTree for {xmlPath}", ex);
                    return(null);
                }
            }
Пример #2
0
        //添加同名节点
        public XmlDoc AddSibling(ConfTree refer, ConfTree tree)
        {
            XmlNode sibling = XmlOp.Find(this, tree.Name);
            if (sibling == ChildNodes[ChildNodes.Count - 1])//非唯一
            {
                RemoveChild(sibling);

                var parent = new ConfTree($"{tree.Name}s");
                parent.Add(refer);
                parent.Add(tree);
                AppendChild(XmlConf.CreateNode(this, parent));
            }
            else
            {
                sibling.ParentNode.AppendChild(XmlConf.CreateNode(this, tree));
            }

            return this;
        }
Пример #3
0
            public static void Save(ConfTree conf, string path = null)
            {
                try
                {
                    XmlDocument doc = conf.XmlDoc as XmlDocument;
                    if (doc != null)
                    {
                        path = path == null?doc.BaseURI.Substring(@"file:///".Length) : path;

                        doc.Save(path);
                    }
                    else
                    {
                        if (path != null)
                        {
                            doc = XmlOp.CreateDoc();
                            doc.AppendChild(XmlConf.CreateNode(doc, conf));
                            doc.Save(path);
                        }
                        else if (!string.IsNullOrEmpty((conf.Refer.XmlDoc as XmlDocument).BaseURI))
                        {
                            doc  = conf.Refer.XmlDoc.AddSibling(conf.Refer, conf);
                            path = doc.BaseURI.Substring(@"file:///".Length);
                            doc.Save(path);
                        }
                        else
                        {
                            throw new Exception($"Invalid param for Save");
                        }

                        conf.XmlDoc = Generate(path).XmlDoc;
                        if (conf.Refer != null)
                        {
                            conf.Refer.XmlDoc = conf.XmlDoc;
                        }
                    }
                }
                catch (Exception ex)
                {
                    _log.Error($"Save({conf.Name}, {path}) failed", ex);
                }
            }
Пример #4
0
 //添加节点
 public void AddNode(string parentPath, ConfItem item)
 {
     parentPath = parentPath.Substring(0, 1) == @"/" ? parentPath.Substring(1) : parentPath;
     var nodes = Find(parentPath.Split('/'));
     nodes[nodes.Length - 1].AppendChild(XmlConf.CreateNode(this, item));
 }