/// <summary> /// Recurtion transfer XmlTag into XmlNode /// </summary> /// <param name="tag">XmlTag</param> /// <returns></returns> public XmlNode TagToNode(XmlTag tag) { XmlNode node = XmlDoc.CreateElement(tag.Name); foreach (var attr in tag.Attrs) { XmlAttribute xmlAttr = XmlDoc.CreateAttribute(attr.Key); xmlAttr.Value = attr.Value; node.Attributes.Append(xmlAttr); } if (tag is XmlChildTag) { XmlChildTag childTag = tag as XmlChildTag; if (childTag.BaseTagList.Count == 0 && childTag.ChildTagList.Count == 0) { throw new Exception("A XmlChildTag " + childTag.Name + "'s BaseTagList and ChildTagList can't both empty,if this tag don't have any child please use XmlBaseTag type"); } //添加对应的子节点 foreach (XmlTag innerTag in childTag.BaseTagList) { node.AppendChild(TagToNode(innerTag)); } foreach (XmlTag innerTag in childTag.ChildTagList) { node.AppendChild(TagToNode(innerTag)); } } else { XmlBaseTag baseTag = tag as XmlBaseTag; node.InnerText = baseTag.InnerText; } return(node); }
/// <summary> /// Create XmlModel from XmlTag and save xml file /// </summary> /// <param name="directory">directory</param> /// <param name="fileName">fileName</param> /// <param name="encode">encode type</param> /// <param name="root">root tag</param> public void CreateWithFile(string directory, string fileName, string encode, XmlTag root) { XmlDirectory = directory; FileName = fileName; Encode = encode; Root = root; Save(); }
/// <summary> /// Recurtion transfer XmlNode into XmlTag /// </summary> /// <param name="node">XmlNode</param> /// <returns></returns> public XmlTag NodeToTag(XmlNode node) { //不转化注释 for (int i = 0; i < node.ChildNodes.Count; i++) { if (node.ChildNodes[i].Name == ("#comment")) { node.RemoveChild(node.ChildNodes[i]); } } //判断当前节点类型返回对应类型节点 if (node.ChildNodes.Count > 0 && node.FirstChild.NodeType != XmlNodeType.Text) { XmlChildTag childTag = new XmlChildTag(node.Name); //属性不为空添加属性 if (node.Attributes != null) { foreach (XmlAttribute attr in node.Attributes) { childTag.Attrs.Add(attr.Name, attr.Value); } } //递归添加子节点 foreach (XmlNode childNode in node.ChildNodes) { XmlTag tag = NodeToTag(childNode); if (tag is XmlChildTag)//根据子节点类型加入对应列表 { childTag.ChildTagList.Add(tag as XmlChildTag); } else { childTag.BaseTagList.Add(tag as XmlBaseTag); } } return(childTag); } else { XmlBaseTag baseTag = new XmlBaseTag(node.Name); //属性不为空添加属性 if (node.Attributes != null) { foreach (XmlAttribute attr in node.Attributes) { baseTag.Attrs.Add(attr.Name, attr.Value); } } baseTag.InnerText = node.InnerText; return(baseTag); } }
/// <summary> /// Constructor: Create XmlModel from XmlTag only /// </summary> /// <param name="root">root tag</param> public XModel(XmlTag root) { CreateWithOutFile(root); }
/// <summary> /// Constructor: Create XmlModel from XmlTag and save xml file /// </summary> /// <param name="directory">directory</param> /// <param name="fileName">fileName</param> /// <param name="encode">encode type</param> /// <param name="root">root tag</param> public XModel(string directory, string fileName, string encode, XmlTag root) { CreateWithFile(directory, fileName, encode, root); }
/// <summary> /// Create XmlModel from XmlTag only /// </summary> /// <param name="root"></param> public void CreateWithOutFile(XmlTag root) { Root = root; }