Exemplo n.º 1
0
        /// <param name="pos">找到Tag之后,Tag的起始位置</param>
        public static XTag FindTag(string tagName, string text, ref int pos)
        {
            Regex reg1 = new Regex("<" + tagName + "[\\s]");
            Regex reg2 = new Regex("<" + tagName + "[\\s\\S]*?>");
            Match m1   = reg1.Match(text, pos);

            if (m1.Success)
            {
                Match m2  = reg2.Match(text, m1.Index);
                XTag  tag = new XTag(text, m2);
                pos = m2.Index;
                return(tag);
            }
            return(null);
        }
Exemplo n.º 2
0
        public XFragment(string text, int start)
        {
            if (text[start] != '<')
            {
                throw new XMLException("XFragment Error:Unexpect Start.");
            }
            indexInSource = start;
            Regex reg_tag = new Regex("<[\\s\\S]*?>");
            int   count = 0, pos = start;
            Match m;

            do
            {
                m = reg_tag.Match(text, pos);
                if (!m.Success)
                {
                    new XMLException("XFragment Error:Unexpect end.");
                }
                XTag tag = new XTag(text, m);
                if (tag.type == PartType.tag_start)
                {
                    count++;
                }
                if (tag.type == PartType.tag_end)
                {
                    count--;
                }
                if (m.Index > pos)
                {
                    parts.Add(new XText(text.Substring(pos, m.Index - pos)));
                }
                parts.Add(tag);
                pos = m.Index + tag.originalText.Length;
            }while (count > 0);
            originalLength = m.Index - start + m.Value.Length;
            root           = new XELement(this, 0);
        }