Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a variable into an xml literal comment

Tags:

xml

vb.net

I'm using xml literals in vb. I would like to insert a variable (derived at runtime) into a comment ie something like;

 Dim vtldMessageBoxes =
            <?xml version="1.0" encoding="UTF-8"?>
            <!--Information about messageboxes. Do not delete this file unless <%= My.Application.Info.Title %> has been deleted. -->
                <Users>
                    <username><%= Environment.UserName %>
                    </username>
                </Users>

If I run this code then the application title does not appear in the comments. Simple question, is it possible to embed variables in xml literal comments at runtime and if so how?

Thanks

like image 673
Dom Sinclair Avatar asked Oct 25 '25 05:10

Dom Sinclair


1 Answers

You are definitely right, you cannot embed expressions in comments in XML literals. This is because the escape characters used for expressions are valid comment characters. The XML comment literal documentation explicitly calls this out:

You cannot use an embedded expression in an XML comment literal because the embedded expression delimiters are valid XML comment content.

To get around this, you will simply have to add the comment to the XDocument that is created by the XML literal:

Dim vtldMessageBoxes =
   <?xml version="1.0" encoding="UTF-8"?>
   <Users>
       <username><%= Environment.UserName %>
       </username>
   </Users>

Dim fullComment = String.Format("Information about messageboxes. Do not delete this file unless {0} has been deleted.", My.Application.Info.Title)

vtldMessageBoxes.AddFirst(New XComment(fullComment))
MessageBox.Show(vtldMessageBoxes.ToString())
like image 67
John Koerner Avatar answered Oct 26 '25 21:10

John Koerner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!