Results 1 to 6 of 6

Thread: Embedded expression in XML literal

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,969

    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.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,969

    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.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: Embedded expression in XML

    Quote Originally Posted by nbrege View Post
    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>
    Quote Originally Posted by nbrege View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,969

    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

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Embedded expression in XML

    Quote Originally Posted by nbrege View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width