views:

67

answers:

1

Is it possible to use variables like <%=person.LastName %> in XML string this way?

XElement letters = new XElement("Letters");

XElement xperson = XElement.Parse("<Table><Row><Cell><Text>
   <Segment>Dear <%=person.Title%> <%=person.FirstName%> <%=person.LastName%>,
   </Segment></Text></Cell></Row>...");

foreach (Person person in persons){
   letters.Add(xperson)
}

If it's possible, it would be a lifesaver since I can't use XElement and XAttribute to add the nodes manually. We have multiple templates and they frequently change (edit on the fly).

If this is not doable, can you think of another way so that I can use templates for the XML?

Look like it's possible in VB http://aspalliance.com/1534_Easy_SQL_to_XML_with_LINQ_and_Visual_Basic_2008.6

A: 

This is an exclusive VB.NET feature known as XML Literals. It was added in VB 9.0. C# does not currently support this feature. Although Microsoft has stated its intent to bridge the gap between the languages in the future, it's not clear whether this feature will make it to C# any time soon.

Your example doesn't seem clear to me. You would want to have the foreach loop before parsing the actual XML since the values are bound to the current Person object. For example, here's a VB example of XML literals:

Dim xml = <numbers> 
            <%= From i In Enumerable.Range(1, 5)
                Select <number><%= i %></number>
            %>
          </numbers>

For Each e In xml.Elements()
    Console.WriteLine(e.Value)
Next

The above snippet builds the following XML:

<numbers>
  <number>1</number>
  <number>2</number>
  <number>3</number>
</numbers> 

Then it writes 1, 2, 3 to the console.

If you can't modify your C# code to build the XML dynamically then perhaps you could write code that traverses the XML template and searches for predetermined fields in attributes and elements then sets the values as needed. This means you would have to iterate over all the attributes and elements in each node and have some switch statement that checks for the template field name. If you encounter a template field and you are currently iterating attributes you would set it the way attributes are set, whereas if you were iterating elements you would set it the way elements are set. This is likely not the most efficient approach but is one solution.

The simplest solution would be to use VB.NET. You can always develop it as a stand-alone project, add a reference to the dll from the C# project, pass data to the VB.NET class and have it return the final XML.

EDIT: to clarify, using VB.NET doesn't bypass the need to update the template. It allows you to specify the layout easier as an XML literal. So code still needs to be updated in VB once the layout changes. You can't load an XML template from a text file and expect the fields to be bound that way. For a truly dynamic solution that allows you to write the code once and read different templates my first suggestion is more appropriate.

Ahmad Mageed