Beispiel #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);
        }
Beispiel #2
0
        /// <summary>
        /// Quickly get a XmlBaseTag
        /// </summary>
        /// <param name="tagNames">按顺序输入的节点名称/tag's name input by order</param>
        /// <returns></returns>
        public XmlBaseTag GetBaseTag(params string[] tagNames)
        {
            if (tagNames[0] != Root.Name)
            {
                throw new Exception("Root tag doesn't match");
            }
            if (Root is XmlBaseTag)
            {
                if (tagNames.Length == 1)
                {
                    return(Root as XmlBaseTag);
                }
                else
                {
                    throw new Exception("Root " + tagNames[0] + " is XmlBaseTag type, don't have any childs");
                }
            }
            XmlChildTag ChildTag = Root as XmlChildTag;

            for (int i = 1; i < tagNames.Length; i++)
            {
                if (ChildTag.ChildTagList.Where(t => t.Name == tagNames[i]).Count() == 0 && ChildTag.BaseTagList.Where(t => t.Name == tagNames[i]).Count() == 0)
                {
                    throw new Exception("Can't find XmlTag " + tagNames[i] + " by the input tag names with this order");
                }
                //最后一个节点查找BaseTagList,否则查找ChildTagList
                if (i == tagNames.Length - 1)
                {
                    if (ChildTag.BaseTagList.Where(t => t.Name == tagNames[i]).Count() > 0)
                    {
                        return(ChildTag.BaseTagList.Where(t => t.Name == tagNames[i]).First());
                    }
                    else
                    {
                        throw new Exception("Tag:" + tagNames[i] + " must be XmlBaseTag type");
                    }
                }
                else
                {
                    //找到对应XmlChildTag节点缩小查找范围
                    if (ChildTag.ChildTagList.Where(t => t.Name == tagNames[i]).Count() > 0)
                    {
                        ChildTag = ChildTag.ChildTagList.Where(t => t.Name == tagNames[i]).First();
                    }
                    else
                    {
                        throw new Exception("Tag:" + tagNames[i] + " must be XmlChildTag type");
                    }
                }
            }
            return(null);
        }
Beispiel #3
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);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Get a XmlChildTag from this tag
        /// </summary>
        /// <param name="tagNames">tag's name input by order</param>
        /// <returns></returns>
        public XmlChildTag GetChildTag(params string[] tagNames)
        {
            XmlChildTag result = this as XmlChildTag;

            for (int i = 0; i < tagNames.Length; i++)
            {
                if (result.ChildTagList.Where(t => t.Name == tagNames[i]).Count() == 0)
                {
                    throw new Exception("Can't find XmlChildTag tag " + tagNames[i] + " by the input tag names with this order");
                }
                result = result.ChildTagList.Where(t => t.Name == tagNames[i]).First();
            }
            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Quickly get a XmlChildTag
        /// </summary>
        /// <param name="tagNames">tag's name input by order</param>
        /// <returns></returns>
        public XmlChildTag GetChildTag(params string[] tagNames)
        {
            if (tagNames[0] != Root.Name)
            {
                throw new Exception("Root tag doesn't match");
            }
            if (!(Root is XmlChildTag))
            {
                throw new Exception("Root " + tagNames[0] + " must be XmlChildTag type");
            }
            XmlChildTag result = Root as XmlChildTag;

            for (int i = 1; i < tagNames.Length; i++)
            {
                if (result.ChildTagList.Where(t => t.Name == tagNames[i]).Count() == 0)
                {
                    throw new Exception("Can't find XmlChildTag tag " + tagNames[i] + " by the input tag names with this order");
                }
                result = result.ChildTagList.Where(t => t.Name == tagNames[i]).First();
            }
            return(result);
        }
Beispiel #6
0
        /// <summary>
        /// Get a XmlBaseTag from this tag
        /// </summary>
        /// <param name="tagNames">tag's name input by order</param>
        /// <returns></returns>
        public XmlBaseTag GetBaseTag(params string[] tagNames)
        {
            XmlChildTag ChildTag = this as XmlChildTag;

            for (int i = 0; i < tagNames.Length; i++)
            {
                if (ChildTag.ChildTagList.Where(t => t.Name == tagNames[i]).Count() == 0 && ChildTag.BaseTagList.Where(t => t.Name == tagNames[i]).Count() == 0)
                {
                    throw new Exception("Can't find XmlTag tag " + tagNames[i] + " by the input tag names with this order");
                }
                //最后一个节点查找BaseTagList,否则查找ChildTagList
                if (i == tagNames.Length - 1)
                {
                    if (ChildTag.BaseTagList.Where(t => t.Name == tagNames[i]).Count() > 0)
                    {
                        return(ChildTag.BaseTagList.Where(t => t.Name == tagNames[i]).First());
                    }
                    else
                    {
                        throw new Exception("Tag:" + tagNames[i] + " must be XmlBaseTag type");
                    }
                }
                else
                {
                    //找到对应XmlChildTag节点缩小查找范围
                    if (ChildTag.ChildTagList.Where(t => t.Name == tagNames[i]).Count() > 0)
                    {
                        ChildTag = ChildTag.ChildTagList.Where(t => t.Name == tagNames[i]).First();
                    }
                    else
                    {
                        throw new Exception("Tag:" + tagNames[i] + " must be XmlChildTag type");
                    }
                }
            }
            return(null);
        }