Reputation: 6526
I have XML Doc like the following Structure.
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<productID></productID>
<productName></productName>
<productDesc></productDesc>
<productFeatures>
<Feature></Feature>
<Feature></Feature>
<Feature></Feature>
</productFeatures>
</product>
<product>
<productID></productID>
<productName></productName>
<productDesc></productDesc>
<productFeatures>
<Feature></Feature>
<Feature></Feature>
<Feature></Feature>
<Feature></Feature>
<Feature></Feature>
<Feature></Feature>
</productFeatures>
</product>
</products>
I succeeded to dispay product element in GridView, but I still looking for a way to display the inner elements in it as following.
var bind = productsDoc.Descendants("products").Select(product => new
{
productID = product.Element("productID").Value,
productName = product.Element("productName").Value,
productDesc = product.Element("productDesc").Value,
productAllFeatures = product.Element("productFeatures").Element("Feature").Value,
}).OrderBy(product => product.productName).ToList();
producsGrdView.DataSource = bind;
producsGrdView.DataBind();
Now by notice the xml file we can see that multiple "Feature" elements belone to the same product, the relation is (many to one).
Also in the code above productAllFeatures display only the first "Feature".
My Question is:
How to load data from many to one realationship, many Features which belong to one product" into the same GridView ?
How to display all Features in producsGrdView as one Grid Column?
is there a simple way to do that?
Upvotes: 0
Views: 685
Reputation: 56696
First step is to turn productAllFeatures
into a list of some sort, say strings:
var bind = productsDoc.Descendants("products").Select(product => new
{
...
productAllFeatures = product.Element("productFeatures")
.Descendants("Feature")
.Select(x => x.Value)
.ToList()
}).OrderBy(product => product.productName).ToList();
Now in the grid view you may want to use inner repeater to bind this list and display it:
<Columns>
...
<TemplateField HeaderText="Features">
<ItemTemplate>
<asp:Repeater runat="server" DataSource='<%# Eval("productAllFeatures") %>'>
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</TemplateField>
I did not test it though, so some minor mistakes might be lurking
Upvotes: 1