XML : Nesting Linq in XML Literals

With a three-level hierarchy of classes (Person, Kid, Toy: A person has one or more kids and each kid has one or more toys), how can one export it to XML using XML Literals? I am using the following code (p is the Person object coming in as parameter):

  Dim X = <Person Name=<%= p.Name %>>    <%= From k In p.GetKids()        Select <Kid Name=<%= k.Name %>>        <%= From t In k.GetToys()            Select <Toy Name=<%= k.Name %> <%= IIf(t.IsBatteryOperated(), "Batteries=""" & t.BatteriesCount & """, "") %> />    </Kid>  </Person>    

I'm trying two things here. For one I'm trying to nest one LINQ-2-XML in another. Second I'm trying to add an optional attribute Batteries that will only appear in the <Toy> nodes that are battery-operated. But this code doesn't compile and I can't figure out what's wrong with it. Does anyone know how can I correct this?

No comments:

Post a Comment