LINQ/XML Literals Problem

Raven65

Well-known member
Joined
Oct 4, 2006
Messages
305
Programming Experience
3-5
Posted this to the MSDN boards but to no avail so far:


I'm using the nifty trick Beth detailed showing how to write XML Excel spreadsheets from LINQ where by I do the following (abreviated):


VB.NET:
Dim exceptions = From letter In allLetters _
Where Not letter.CleanExceptionReason = "" _

Select <Row>
  <Cell><Data ss:Type="String"></Data></Cell>
  <Cell><Data ss:Type="String"></Data></Cell>
</Row>

Dim exceptionsReport = <?xml version="1.0"?>
[whole bunch of Excel formatting things]
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedRowCount= x:FullColumns="1" x:FullRows="1">
 <Row ss:StyleID="s21">
  <Cell><Data ss:Type="String">Var1</Data></Cell>
  <Cell><Data ss:Type="String">Var2</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>

So, that work's all nice, but I have a problem when I want to build multiple rows per entry. What I want to achive is:

VB.NET:
Dim exceptions = From letter In allLetters _
Where Not letter.CleanExceptionReason = "" _
Select <Row>
  <Cell><Data ss:Type="String"><%= myVar1 %></Data></Cell>
  </Row><Row>
  <Cell><Data ss:Type="String"><%= myVar2 %></Data></Cell>
  </Row>


So that each "letter" from my list generates two <Row>'s of data in the exceptions variable, to be injected into the XML below. VB's interpretation of the XML literal seems to be preventing me from doing this however....any thoughts?
 
Looks like each XElement must have a single root node. Depending on usage you can make two selections one for each row and later get one row from each collection for each element, or wrap the two rows with a root node then get exceptions...<Row> to get a collection of all Row elements in sequence.
 
Yea, the second solution there is what I went with for now, but it just feels so sloppy. =\

I think I'm with you though, there appears to be no legit way to do this given the conventions of the XElement.
 
Back
Top