views:

19

answers:

1

I am creating an xml file with LINQ as follows...

   Public Sub CreateXml()
        Dim db As New MDataContext
        Dim Customers = <gallery columns="3" rows="3">
                            <%= From customer In db.Customers _
                                Select <customer>
                                           <name><%= customer.CustName %></name>
                                           <surname><%= customer.Surname %></surname>
                                       </customer> %>
                        </gallery>
        Customers.Save("d:\1.xml")
    End Sub

Could i mix local values with the ones returned from the LINQ query... Something like the following?

 Public Sub CreateXml(ByVal **Col** As String, ByVal **Row** As String)
        Dim db As New MDataContext
        Dim Customers = <gallery columns="& **Col** &" rows="& **Row** &">
                            <%= From customer In db.Customers _
                                Select <customer>
                                           <name><%= customer.CustName %></name>
                                           <surname><%= customer.Surname %></surname>
                                       </customer> %>
                        </gallery>

        Customers.Save("d:\1.xml")
        Process.Start("d:\1.xml")
    End Sub
+3  A: 

You should be able to embed the values of the two variables just as you are doing with the customer.CustName and customer.Surname, e.g.

Public Sub CreateXml(ByVal cols As Integer, ByVal rows As Integer)
    Dim db As New MDataContext
    Dim Customers = <gallery columns="<%= cols %>" rows="<%= rows %>">
                        <%= From customer In db.Customers _
                            Select <customer>
                                       <name><%= customer.CustName %></name>
                                       <surname><%= customer.Surname %></surname>
                                   </customer> %>
                    </gallery>

    Customers.Save("d:\1.xml")
    Process.Start("d:\1.xml")
End Sub

Strictly speaking, you should probably make an appropriate ToString() call, e.g. <%= cols.ToString(CultureInfo.InvariantCulture) %>. This way you ensure that the change doesn't introduce any unnecessary boxing and is also independent of the culture of the executing thread, although the latter is not likely to have any affect with an Integer variable.

AdamRalph