Click here to Skip to main content
15,914,160 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I have some records coming from the XML.
and i have to show these records into grid view.

but my problem is to pass them to an array having column name and values,
i.e an array having column name and values

so that i can assign a data source to grid view.
i am not too sure about how to do it?


here is the XML.


<?xml version="1.0"?><layout schemaVersion="1" width="800" height="600" bgcolor="#000000" bgImage="128f427e-73ab-4bbf-96c7-ee3b4a4ed67b_thumb.jpg <region id="f1f9e768-72be-408a-803f-8dacbade446c" width="590" height="601" top="0" left="211"><media id="193" XmlMediaId="67e9dfff-b224-4394-aab3-23eb18b7911d" type="Video" duration="12" schemaVersion="1"><options><uri>67e9dfff-b224-4394-aab3-23eb18b7911d_thumb.avi</uri></options><raw /></media><media id="177" XmlMediaId="fa0c7afc-2e1e-46a1-96ad-18c4518808b1" type="Image" duration="12" schemaVersion="1"><options><uri>128f427e-73ab-4bbf-96c7-ee3b4a4ed67b_thumb.jpg</uri></options><raw /></media><media id="173" XmlMediaId="6e0d06f4-5fda-4167-89f3-ffe859d503ef" type="Image" duration="12" schemaVersion="1"><options><uri>180f0adb-ce5a-476f-8d04-8a14153b7c9a_thumb.jpg</uri></options><raw /></media><media id="174" XmlMediaId="889a5000-2ada-40d9-8a02-ce202d6b8481" type="Image" duration="12" schemaVersion="1"><options><uri>44ba8490-8dd8-44e5-b7ec-d56fc0d5e378_thumb.jpg</uri></options><raw /></media><media id="176" XmlMediaId="5a32e8a8-0253-4bb3-ae4a-a39dd5b97d80" type="Image" duration="12" schemaVersion="1"><options><uri>20d350aa-7af7-4f6b-adbd-b2dbadefa0f0_thumb.jpg</uri></options><raw /></media><media id="177" XmlMediaId="6aa2501a-3fd7-4d0d-8522-0f30bd188f8e" type="Image" duration="12" schemaVersion="1"><options><uri>128f427e-73ab-4bbf-96c7-ee3b4a4ed67b_thumb.jpg</uri></options><raw /></media><media id="186" XmlMediaId="43e76e4e-1118-4160-982e-148d1fd868d5" type="Image" duration="12" schemaVersion="1"><options><uri>43e76e4e-1118-4160-982e-148d1fd868d5_thumb.jpg</uri></options><raw /></media><media id="191" XmlMediaId="aebead68-18a2-4c1c-991f-353d379d61fb" type="Image" duration="12" schemaVersion="1"><options><uri>aebead68-18a2-4c1c-991f-353d379d61fb_thumb.jpg</uri></options><raw /></media><media id="196" XmlMediaId="93759edf-2112-4526-83b2-db65e5445ca1" type="Image" duration="12" schemaVersion="1"><options><uri>93759edf-2112-4526-83b2-db65e5445ca1_thumb.jpg</uri></options><raw /></media><media id="195" XmlMediaId="d78a710d-52b6-46b6-9572-299464263f6e" type="Video" duration="223" schemaVersion="1"><options><uri>d78a710d-52b6-46b6-9572-299464263f6e_thumb.avi</uri></options><raw /></media><media id="199" XmlMediaId="ada28fa0-6351-4214-9253-aa451733ea4e" type="Image" duration="3434" schemaVersion="1"><options><uri>ada28fa0-6351-4214-9253-aa451733ea4e_thumb.jpg</uri></options><raw /></media></region></layout>



and i need something like this.

Id Type Duration

193 Video 12
177 Image 12
173 Image 12


and so on...


Thanks in advance.
Posted
Updated 12-Jan-11 1:09am
v6

oh! its good that you edit your question and add XML code.
you can use given code
XML
<asp:GridView ID="GridView1" runat="server" Width="507px" AutoGenerateColumns="False" DataSourceID="XmlDataSource1">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="Id" SortExpression="id" />
                <asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
                <asp:BoundField DataField="duration" HeaderText="Duration" SortExpression="duration" />
            </Columns>
        </asp:GridView>
        &nbsp;&nbsp;
           <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml"></asp:XmlDataSource> // you can set your XML path 
 
Share this answer
 
Comments
yorker129 12-Jan-11 5:20am    
But it is dynamic XML and not a file on the application.

The XML is stored in the database.
RaviRanjanKr 12-Jan-11 5:29am    
you should Explain it in your question because user take your XML code as a XML file as I do.
Hope you will edit your question.
yorker129 12-Jan-11 7:08am    
Let me explain.

The XML is coming from the database so i need to do it in coding.
I have to read the XML Nodes 1 by 1 and may be insert them into a data source. when all the attributes will be inserted then i need to assign that data source to the grid view.
The main concern is where to maintain those attributes from the XML.
Into an array having column name? (i.e multi dimensional array)

i cant use the above code because all i need to do in the coding.
Use of LINQ to XML datatype[^] for getting appropriate result for source of gridview. See Linq query for XML at the bottom of article.

Thanks
 
Share this answer
 
you can simply done it by binding gridview with your XML file. look at this
Link[^] and this link2(Step-by-Step)[^] it might help you.

let use know if any problem.
 
Share this answer
 
Comments
yorker129 12-Jan-11 1:31am    
Thanks for answering.

but i want to show some attributes from the XML in the grid view and i have to first pass each attribute one by one into a data source and then assign that source to the grid view.

and also tried it and gives the error "Illegal characters in path."
You can using for XSLT. More than one rows bind to single column.

example XSL

XML
<xsl:element name="DataGridColumnName">
                    <xsl:for-each select="DataGridColumnName">
                        <xsl:value-of select="RowName"/><xsl:value-of select="SeatNo"/>
                        <xsl:if test="position() != last()">
                           <xsl:text>, </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                    </xsl:element>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900