-
May 19th, 2023, 08:22 AM
#1
Thread Starter
Frenzied Member
Embedded expression in XML literal
I'm trying to build an XML document. I have a literal with an embedded expression in a module. (see first code snippet)
I'm trying to replace the expression with a value in a button click event on the main form. (see second code snippet)
The expression is not being replaced as desired. Can someone show me the proper way to do this? Thanks...
Code:
Module Module1
Public empname As String
Public employee As XElement =
<employee>
<name><%= empname %></name>
</employee>
End Module
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
empname = "John"
Console.WriteLine(employee)
End Sub
Output:
<employee>
<name />
</employee>
Desired output:
<employee>
<name>John</name>
</employee>
Last edited by nbrege; May 19th, 2023 at 09:09 AM.
-
May 19th, 2023, 08:27 AM
#2
Re: Embedded expression in XML
The use of a Module???? has me confused.
Code:
Public Function NameIT(name As String) As XElement
Dim employee As XElement
employee = <employee>
<name><%= name %></name>
</employee>
Return employee
End Function
-
May 19th, 2023, 08:32 AM
#3
Thread Starter
Frenzied Member
Re: Embedded expression in XML
I want to keep my literals separate from my main code. So I need to put it in a function?
Last edited by nbrege; May 19th, 2023 at 08:36 AM.
-
May 19th, 2023, 08:40 AM
#4
Re: Embedded expression in XML
Originally Posted by nbrege
I'm trying to build an XML document. I have a literal with an embedded expression in a module. (see first code snippet)
I'm trying to replace the expression with a value in a button click event on the main form. (see second code snippet)
The expression is not being replaced as desired. Can someone show me the proper way to do this? Thanks...
Code:
Module Module1
Public empname As String
Public employee As XElement =
<employee>
<name><%= empname %></name>
</employee>
End Module
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim empname As String = "John"
Console.WriteLine(employee)
End Sub
Output:
<employee>
<name />
</employee>
Desired output:
<employee>
<name>John</name>
</employee>
Originally Posted by nbrege
I want to keep my literals separate from my main code. So I need to put it in a function?
It's a matter of scope ... you have emplyee defined in a module ... in that same module you defined empname ... but then in your sub, you defined a NEW empname ... it is not the same as the one in the module. My guess would be that if you used the module empname, it might work. If it doesn't then use a function like dbasnett showed ... you could put the function into the module if you want.
-tg
-
May 19th, 2023, 09:11 AM
#5
Thread Starter
Frenzied Member
Re: Embedded expression in XML
TG ... you are right. Not sure why I created a new empname variable. I corrected my code, but it still doesn't work.
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
empname = "John"
Console.WriteLine(employee)
End Sub
-
May 19th, 2023, 10:05 AM
#6
Re: Embedded expression in XML
Originally Posted by nbrege
I want to keep my literals separate from my main code. So I need to put it in a function?
Did you mean module?
Still not sure what you think module does for you. I avoid them like the plague.
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim foo As XElement = <Employees>
<%= ProtectedXElementLiterals.EmployeeLiteral("Dewayne") %>
</Employees>
Stop
End Sub
End Class
Friend Class ProtectedXElementLiterals
Public Shared Function EmployeeLiteral(name As String) As XElement
Dim employee As XElement
employee = <employee>
<name><%= name %></name>
</employee>
Return employee
End Function
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|