public static ConfTree ToTree(XmlNode node, XmlDoc doc = null) { var result = new ConfTree(node.Name); result.XmlDoc = doc; var tag = (node as XmlElement).Attributes.GetNamedItem("tag"); if (tag != null) { result.Tag = tag.Value; } foreach (XmlAttribute attr in (node as XmlElement).Attributes) { result.Attributes[attr.Name] = attr.Value; } foreach (XmlNode n in node.ChildNodes) { if (IsItem(n)) { result.Add(ToItem(n)); } else { if (n.ChildNodes.Count > 1) { result.Add(ToTree(n, doc)); } else if (n.ChildNodes.Count == 1 && n.ChildNodes[0] is XmlElement) { result.Add(ToTree(n, doc)); } else { var ele = (n as XmlElement); if (ele != null && ele.Attributes.GetNamedItem("tag") != null) { result.Add(ToTree(n, doc)); } } } } return(result); }
//添加同名节点 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; }
public static ConfTree Generate(Dictionary <string, string> kvs, string name) { ConfTree result = new ConfTree(name); result.Source = Source.Dictionary; result.Tag = "Default"; foreach (var kv in kvs) { result.Add(new ConfItem(kv.Key, kv.Value)); } return(result); }
public void Save(string path = null) { if (path == null) { foreach (var conf in Root.Values) { Builder.Xml.Save(conf, path); } } else { ConfTree root = new ConfTree("Root"); foreach (var conf in Root.Values) { root.Add(conf); } Builder.Xml.Save(root, path); } }
public override ConfItem Clone(string tag = null) { var conf = new ConfTree(Name) { Source = Source, Value = Value, Tag = tag == null ? Tag : tag, Attributes = Attributes, Refer = this, }; foreach (var son in Sons) { conf.Add(son.Clone()); } return(conf); }