Beispiel #1
0
        /// <summary>
        /// Replace words in a document.
        /// </summary>
        /// <param name="wordlist">List of words to run replacement function for.</param>
        /// <param name="handler">Word replacement delegate.</param>
        public static void ReplaceText(XDocWord[] wordlist, ReplacementHandler handler)
        {
            if (wordlist == null)
            {
                throw new ArgumentNullException("wordlist");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            // loop through words backwards
            for (int i = wordlist.Length - 1; i >= 0; --i)
            {
                XDocWord word = wordlist[i];
                if (word.IsText)
                {
                    XmlNode replacement = handler(word.Node.OwnerDocument, word);
                    if (replacement != null)
                    {
                        // split the node
                        XmlText node = (XmlText)word.Node;

                        // split off the part after the text
                        if (node.Value.Length > (word.Offset + word.Value.Length))
                        {
                            node.SplitText(word.Offset + word.Value.Length);
                        }

                        // split off the part before the text
                        if (word.Offset > 0)
                        {
                            node = node.SplitText(word.Offset);
                        }

                        // replace text with new result
                        node.ParentNode.InsertAfter(replacement, node);
                        node.ParentNode.RemoveChild(node);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Replace words in a document.
        /// </summary>
        /// <param name="wordlist">List of words to run replacement function for.</param>
        /// <param name="handler">Word replacement delegate.</param>
        public static void ReplaceText(XDocWord[] wordlist, ReplacementHandler handler)
        {
            if(wordlist == null) {
                throw new ArgumentNullException("wordlist");
            }
            if(handler == null) {
                throw new ArgumentNullException("handler");
            }

            // loop through words backwards
            for(int i = wordlist.Length - 1; i >= 0; --i) {
                XDocWord word = wordlist[i];
                if(word.IsText) {
                    XmlNode replacement = handler(word.Node.OwnerDocument, word);
                    if(replacement != null) {

                        // split the node
                        XmlText node = (XmlText)word.Node;

                        // split off the part after the text
                        if(node.Value.Length > (word.Offset + word.Value.Length)) {
                            node.SplitText(word.Offset + word.Value.Length);
                        }

                        // split off the part before the text
                        if(word.Offset > 0) {
                            node = node.SplitText(word.Offset);
                        }

                        // replace text with new result
                        node.ParentNode.InsertAfter(replacement, node);
                        node.ParentNode.RemoveChild(node);
                    }
                }
            }
        }