I'm deserializing data from an XML file in order to show the data in a gridview. Here is the XML file content:
<Params>
<Param name="Paramètre #1">
<Value> 1.1 </Value>
<Value> 1.2 </Value>
<Value> 1.3 </Value>
</Param>
<Param name="Paramètre #2">
<Value> 2.1 </Value>
<Value> 2.2 </Value>
</Param>
<Param name="Paramètre #3">
<Value> 3.1 </Value>
<Value> 3.2 </Value>
<Value> 3.3 </Value>
<Value> 3.4 </Value>
</Param>
</Params>
I would like the gridview to rend like this:
All i've been able to do is this:
Is there a way to store any value of a parameter in a column whatever the number of values of a parameter?
Here is the Markup:
<html lang="fr">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<form runat="server">
<asp:Label runat="server" ID="Lb" ></asp:Label>
<asp:GridView runat="server" ID="Gdv" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Parameters" DataField="Name"></asp:BoundField>
<asp:TemplateField HeaderText="Values">
<ItemTemplate>
<asp:Label ID="LbGrid" runat="server" Text="<%# GetValues(Container.DataItem) %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
And here is the code-behind:
public partial class SiteMaster : MasterPage
{
List<Param> Parameters { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(@"D:\Utilisateurs\valen\Desktop\ParamsTab\XML\Main.xml");
XmlSerializer deserializer = new XmlSerializer(typeof(List<Param>), new XmlRootAttribute("Params"));
Parameters = (List<Param>)deserializer.Deserialize(sr);
Gdv.DataSource = Parameters;
Gdv.DataBind();
sr.Close();
}
public string GetValues(object param)
{
Param parameter = (Param)param;
string values = string.Empty;
values += string.Join(",", parameter.Values);
return values;
}
}
And here is the class used as a datasource:
public class Param
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("Value")]
public List<string> Values { get; set; }
}