如何将变量插入XML Literals注释部分并对其进行评估?

时间:2010-03-26 17:42:02

标签: xml vb.net

如何在评论部分填充变量My.Application.Info.Version.ToString?

    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>

2 个答案:

答案 0 :(得分:0)

如果你要反汇编这段代码:

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

你会看到VB正在为你做一些工作:

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

我是XML文字的新手,因此可能有一个简单的技巧可以将XComment插入到XML中,或者您可以像在C#中一样构建XML:

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将显示为:

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

答案 1 :(得分:0)

您可以插入像这样的评论

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()))