unknownsatan
unknownsatan

Reputation: 731

Loading images from XML in ASP.NET

I have this XML file, say (Sample.xml)

<?xml version="1.0" encoding="utf-8" ?>

<drinks>
  <drink>
    <type>Coke </type>
    <image>some.jpg or (url) </image>
    <price> $5 </price>
  </drink>
  <drink>
    <type>Pepsi</type>
    <image>some.jpg or (url) </image>
    <price> $2 </price>
  </drink>
  </drinks>

I have a list box too, which is binded to the XML file.

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" 
        DataSourceID="XmlDataSource2" DataTextField="drinks/drink/type" Height="198px" Width="181px">
    </asp:ListBox>
    <asp:XmlDataSource ID="XmlDataSource2" runat="server" DataFile="~/XMLFile.xml">
    </asp:XmlDataSource>

How can I do this? Well, to explain my problem clearly, I want to do this, Products and links to images should load from an xml file (images should be url’sfrom the web)

Upvotes: 0

Views: 3554

Answers (2)

shallee Manivannan
shallee Manivannan

Reputation: 1

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataSourceID="XmlDataSource1">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
           <asp:Label ID="Label1" runat="server" Text='<%Eval("name")%>'>
                   </asp:Label>                                          
       <asp:Image ID="Image1" runat="server" Height="43px" ImageUrl='<%# Eval("image" )%>' Width="35px" />       
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94643

There are number of ways to do so but you can't list/display type and image with ListBox control. You should have to use GridView/DataList/Repeater to bind the XMLDatasource where you may show data along with images.

Assume that the <image> tag contains relative url of an image.

<?xml version="1.0" encoding="utf-8" ?>
<drinks>
  <drink>
    <type>Coke </type>
    <image>~/Images/image1.png</image>
    <price> $5 </price>
  </drink>
  ....
</drinks>

and the ASP.NET markup would be:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="XmlDataSource1">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" 
                           runat="server" 
                           Text='<%# XPath("type") %>'></asp:Label>
                <asp:Image ID="Image1"
                           runat="server" Height="43px" 
                           ImageUrl='<%# XPath("image") %>' 
                           Width="35px" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml" 
    XPath="drinks/drink"></asp:XmlDataSource>

You can also choose the Linq-XML to parse the XML document and prepare the anonymous or typed List<T>.

XDocument doc = XDocument.Load(MapPath("~/XMLFile.xml"));
 var result = from ele in doc.Descendants("drink")
               select new
                 {
                   Type=ele.Element("type").Value,
                   Image=ele.Element("image").Value
                 };

 GridView1.DataSource = result.ToList();
 GridView1.DataBind();

Upvotes: 2

Related Questions