Пример #1
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;
        }
Пример #2
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);
                }
            }