예제 #1
0
        /// <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);
        }
예제 #2
0
 /// <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);
     }
 }