tags:

views:

42

answers:

2

How can I get the variable My.Application.Info.Version.ToString to populate in the comments section?

    Dim Customers As XDocument = <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                                 <!--Application Version: <%= My.Application.Info.Version.ToString %>-->
                                 <customers>
                                     <customer>
                                         <LastName>Jones</LastName>
                                     </customer>
                                     <customer>
                                         <LastName>Baggins</LastName>
                                         <FirstName>Billbo</FirstName>
                                     </customer>
                                     <customer>
                                         <LastName>Baggins</LastName>
                                         <FirstName>Frodo</FirstName>
                                     </customer>
                                     <customer>
                                         <LastName>Kurata</LastName>
                                         <FirstName>Deborah</FirstName>
                                     </customer>
                                 </customers>
A: 

If you were to disassemble this code:

Sub Main()
    Dim doc As XDocument = <?xml version="1.0" encoding="utf-8"?>
                           <!--Application Version: VERSION HERE-->
                           <SEPUnits></SEPUnits>

    doc.Save("test.xml")
End Sub

You would see that VB is just doing some of the work for you:

Public Shared Sub Main()
    Dim VB$t_ref$S0 As New XDocument(New XDeclaration("1.0", "utf-8", Nothing), Nothing)
    VB$t_ref$S0.Add(New XComment("Application Version: VERSION HERE"))
    Dim VB$t_ref$S1 As New XElement(XName.Get("SEPUnits", ""))
    VB$t_ref$S1.Add("")
    VB$t_ref$S0.Add(VB$t_ref$S1)
    VB$t_ref$S0.Save("test.xml")
End Sub

I'm new to XML literals, so there may be an easy trick to insert an XComment into your XML, or you can just build the XML like they do in C#:

Dim version As String = "1.0.0.1"
Dim doc As New XDocument(New XDeclaration("1.0", "utf-8", Nothing), New XComment("Application Version: " & Version), New XElement("customers"))

Doc.ToString would display as:

<?xml version="1.0" encoding="utf-8" ?> 
<!-- Application Version: 1.0.0.1--> 
<customers />
Ocelot20
A: 

You can insert the comment like this

Dim Customers As XDocument = <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                     <customers>
                         <customer>
                             <LastName>Jones</LastName>
                         </customer>
                         <customer>
                             <LastName>Baggins</LastName>
                             <FirstName>Billbo</FirstName>
                         </customer>
                         <customer>
                             <LastName>Baggins</LastName>
                             <FirstName>Frodo</FirstName>
                         </customer>
                         <customer>
                             <LastName>Kurata</LastName>
                             <FirstName>Deborah</FirstName>
                         </customer>
                     </customers>

Customers.Root.AddBeforeSelf(New XComment("Application Version: " & My.Application.Info.Version.ToString()))
Anthony Pegram
Thanks. I'll give it a go!
cjirka
It works. Exactly what I was looking for.
cjirka