Correct way to insert XMLElement objects?

SyntaxEditor Web Languages Add-on for Windows Forms Forum

Posted 16 years ago by Craig Presti
Version: 4.0.0262
Avatar
We recently purchased the Web languages add-on, primarily for schema supported XML editing. Part of our application generates new XmlElements (as part of an underlying XmlDocument) and I need to be able to dynamically insert the new elements into the text representation to synchronise both data stores.

Currently, Im trying to perform the element addition by inserting the XmlElement.OuterXml text into the current document, so I look for the last index of the </ which is the outermost closing element. Im not sure if I have come across a bug or if im doing something incorrectly, but it seems to work ok the first time, but everytime afterwards inserts the text into the wrong position
_SyntaxEditor.Document.InsertText(DocumentModificationType.Custom, _SyntaxEditor.Document.Text.LastIndexOf("</") - 2, xml); 
Is there a better way for performing this sync? I briefly looked into the AstNode documentation and it looked like it may be useful for this purpose?

Comments (3)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

I think the problem is Document.Text. Remember that Document.Text reapplies CRLF as line terminators (instead of LF-only which is used by SyntaxEditor internally for speed). So your LastIndexOf will likely be out of bounds of the document length. Instead you can use Document.GetText() and specify the line terminator only. Then use LastIndexOf on that.

And an even better way of finding this offset which will run faster is to call Document.GetTextStream and navigate to the end of the document, then use its token searching feature to search backwards for a '/>' token. That doesn't require the construction of the entire document (as Text, GetText() do) so will run faster.

Hope that helps!


Actipro Software Support

Posted 16 years ago by Craig Presti
Avatar
Thanks, works great!

This is the code I ended up using:


TextStream stream = _SyntaxEditor.Document.GetTextStream(0);
                stream.GoToDocumentEnd();
                
                while(stream.TokenText != "</")
                    stream.ReadTokenReverse();

                _SyntaxEditor.Document.InsertText(DocumentModificationType.Custom, stream.Offset, xml);

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Craig, just make sure watch for an infinite loop condition in your while loop where there is no token with "</". Also if you know the ID of the token you are looking for or key, you can use TextStream.GoToPreviousxxx methods where it does it all for you.


Actipro Software Support

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.