Linq, XML and Repeater issues.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<br/>

Im new to Linq and have a problem returning multiple records from an XML database that no solutions on Bing or Google has been able to solve. I am only able to return the first child elements ("Variable") attributes which
is under the "Variables" element (XML sample below) even when there are more than one. I have no problem getting the elements under the "Set" parent element when filtering by its ID attribute. Within my foreach query no matter what
I try it only returns one record.
The code is:

<pre class="prettyprint /// <summary>
/// Gets child records under the "Variables" element and populates a repeater
/// </summary>
/// <param name="parentId The "Set" ID attribute</param>
protected void BindVariableRepeater(string parentId)
{

XDocument root = XDocument.Load(DMEFramework.DMEUtilities.xmlFileLocation());
var rec = from c in root.Descendants("Set")
where c.Attribute("ID").Value == "" + parentId + ""
select new
{
DisplayName = c.Element("Variables").Element("Variable").Attribute("DisplayName").Value

};

foreach (var obj in rec)
{

// Response.Write(obj.DisplayName);
}
this.rptVars.DataSource = rec;
this.rptVars.DataBind();

}[/code]
The ASPX HTML:
<pre class="prettyprint <asp:Repeater ID="rptVars" runat="server
<ItemTemplate>


<asp:Label ID="lblSelectedset" runat="server" Text=<%# Eval("DisplayName")%>></asp:Label>


<asp:TextBox ID="txtVarValue" runat="server </asp:TextBox>


</ItemTemplate>
</asp:Repeater>[/code]

The XML:
<pre class="prettyprint <DirectSmileSets> <Set ID="1" Name="BeerGlass" DisplayName="Beer Glass
<Size>
<Width>300</Width>
<Height>200</Height>
<Resolution>72</Resolution>
</Size>
<Variables>
<Variable Name="FirstName" DisplayName="First Name" AllowSplit="False </Variable>
<Variable Name="LastName" DisplayName="Last Name" AllowSplit="False </Variable>
</Variables>
</Set>
<Set ID="2" Name="Leaves" DisplayName="Leaves
<Size>
<Width>300</Width>
<Height>200</Height>
<Resolution>72</Resolution>
</Size>
<Variables>
<Variable Name="FirstName" DisplayName="First Name" AllowSplit="False </Variable>
</Variables>
</Set>
</DirectSmileSets> [/code]
<br/>



<a> <span style="font-size:small

<span style="font-size:small <span style="font-size:small
<span class="x_t-marker
<span style="font-size:small <span style="font-size:small

<br/>


<span style="font-size:small


View the full article
 
Back
Top