XML图像如何在c#.net中的数据网格列显示

时间:2017-10-20 03:55:55

标签: c# xml

这是xml代码 这个图像我必须显示datagridview colums //cdn.apixu.com/weather/64x64/day/113.png

<root>
<condition>
<text>Sunny</text>
<icon>//cdn.apixu.com/weather/64x64/day/113.png</icon>
<code>1000</code>
</condition>
</root>

这是我在c#.net中尝试过的代码。所有数据都将正确显示但图像未显示唯一路径正在datagridview.how显示以在datgridview显示图像

DataTable dt = new DataTable();
            dt.Columns.Add("country", typeof(string));
            dt.Columns.Add("Date", typeof(string));
            dt.Columns.Add("Max Temp", typeof(string));
            dt.Columns.Add("Min Temp", typeof(string));
            dt.Columns.Add("Text", typeof(string));
            dt.Columns.Add("Icon", typeof(string));

            city = txttext.Text;

            string uri = string.Format("http://api.apixu.com/v1/forecast.xml?={0}&days=7", city);

            XDocument doc = XDocument.Load(uri);
            foreach (var npc in doc.Descendants("forecastday"))
            {

                dt.Rows.Add(new object[] {
                         (string)doc.Descendants("country").FirstOrDefault(),
                           (string)npc.Descendants("date").FirstOrDefault(),
                           (string)npc.Descendants("maxtemp_c").FirstOrDefault(),
                           (string)npc.Descendants("mintemp_c").FirstOrDefault(),
                           (string)npc.Descendants("text").FirstOrDefault(),
                           "http:" +(string)npc.Descendants( "icon").FirstOrDefault()
                       });

            }
            dataGridView1.DataSource = dt;

1 个答案:

答案 0 :(得分:0)

使用您的密钥测试代码,然后将其删除。这是我第三次帮助这个项目。或者它是第4个?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            string city = "London";
            string uri = string.Format("http://api.apixu.com/v1/forecast.xml?key=MyKey&q={0}&days=7", city);

            XDocument doc = XDocument.Load(uri);

            List<weather> we = new List<weather>();
            we.Add(new weather() { 
                Maxtemp = (string)doc.Descendants("maxtemp_c").FirstOrDefault(),
                Mintemp = (string)doc.Descendants("mintemp_c").FirstOrDefault()
            });

            dataGridView1.DataSource = we;

        }
    }

    public class weather
    {
        private string date;
        private string maxtemp;
        private string mintemp;
        private string maxwindmph;
        private string maxwindkph;
        private string humidity;

        public weather() { }
        public weather(string maxtemp, string mintemp, string maxwindmph, string maxwindkph, string humidity)
        {

            this.maxtemp = maxtemp;
            this.mintemp = mintemp;
            this.maxwindmph = maxwindmph;
            this.maxwindkph = maxwindkph;
            this.humidity = humidity;


        }


        public string Maxtemp
        {
            get { return maxtemp; }
            set { maxtemp = value; }
        }
        public string Mintemp
        {
            get { return mintemp; }
            set { mintemp = value; }
        }
        public string Maxwindmph
        {
            get { return maxwindmph; }
            set { maxwindmph = value; }
        }
    }
}