XMLLiteral - Generate Dynamic Attributes

  • Thread starter Thread starter Rory Becker
  • Start date Start date
R

Rory Becker

I need to generate an XElement with an indeterminate number of attributes
whose names and values will have previously have been added to a dictionary(Of
String, String)

-------------------------------------------------------------
Private Attributes As New Dictionary(Of String, String)
Public Overrides Function GenerateXElement() As XElement
Return <DocElement>
<InnerElement <%= From Attribute In Attributes Select
SomethingHereIGuess %> />
</DocElement>
End Function
 
Rory said:
I need to generate an XElement with an indeterminate number of
attributes whose names and values will have previously have been added
to a dictionary(Of String, String)

-------------------------------------------------------------
Private Attributes As New Dictionary(Of String, String)
Public Overrides Function GenerateXElement() As XElement
Return <DocElement>
<InnerElement <%= From Attribute In Attributes
Select SomethingHereIGuess %> />
</DocElement>
End Function

The following might show what you are looking for:

Dim col As Dictionary(Of String, String) = New Dictionary(Of
String, String)()
col.Add("att1", "value 1")
col.Add("att2", "value 2")

Dim foo As XElement = <foo <%= From p In col Select New
XAttribute(p.Key, p.Value) %>></foo>
foo.Save(Console.Out)


Result is <foo att1="value 1" att2="value 2"></foo>
 
Hello Martin,
The following might show what you are looking for:

Dim col As Dictionary(Of String, String) = New Dictionary(Of
String, String)()
col.Add("att1", "value 1")
col.Add("att2", "value 2")
Dim foo As XElement = <foo <%= From p In col Select New
XAttribute(p.Key, p.Value) %>></foo>
foo.Save(Console.Out)
Result is <foo att1="value 1" att2="value 2"></foo>

Thanks very much Martin. That looks great.
 
Back
Top