Создания цикла для вывода информации - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Resolvers;
using System.Xml.Schema;
using System.Xml.Serialization.Advanced;
using System.Xml.Serialization.Configuration;
using System.Xml.XmlConfiguration;
using System.Xml.XPath;
using System.Xml.Xsl;
using xNet.Net;
using xNet.Text;
using System.IO;
namespace WindowsFormsApplication5
{
public partial class Form5 : Form
{
List<Image> images;
public Form5()
{
InitializeComponent();
WebRequest reqGET = WebRequest.Create("https://api.vk.com/method/friends.get.xml?user_id=211344927&fields=nickname,photo_200_orig");//запрос
WebResponse resp = reqGET.GetResponse();//ответ
Stream stream = resp.GetResponseStream();//ответ в поток
XmlDocument doc = new XmlDocument();
doc.Load(stream);
XmlNode user = doc.GetElementsByTagName("user")[0];
string status = user.ChildNodes[5].InnerText;
string fn = user.ChildNodes[1].InnerText;
string ln = user.ChildNodes[2].InnerText;
string photo = user.ChildNodes[4].InnerText;
label1.Text = status;
label2.Text = fn;
label3.Text = ln;
label4.Text = photo;
XmlNode user1 = doc.GetElementsByTagName("user")[1];
string status1 = user1.ChildNodes[5].InnerText;
string fn1 = user1.ChildNodes[1].InnerText;
string ln1 = user1.ChildNodes[2].InnerText;
string photo1 = user1.ChildNodes[4].InnerText;
label5.Text = status1;
label6.Text = fn1;
label7.Text = ln1;
label8.Text = photo1;
stream.Close();
resp.Close();
var links = new string[]
{
""+ label4.Text +"",
""+ label8.Text +""
};
images = new List<Image>();
foreach (var url in links)
using (var stream1 = new WebClient().OpenRead(url))
images.Add(Bitmap.FromStream(stream1));
}
protected override void OnPaint(PaintEventArgs e)
{
var y = 20;
foreach(var img in images)
{
e.Graphics.DrawImage(img, new Rectangle(10, y, 100, 100));
y += 120;
}
}
private void Form5_Load(object sender, EventArgs e)
{
}
}
}Решение задачи: «Создания цикла для вывода информации»
textual
Листинг программы
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.Linq;
namespace WindowsFormsApplication251
{
public partial class Form1 : Form
{
Users users;
private const int ItemHeight = 100;
public Form1()
{
InitializeComponent();
using (var st = new WebClient().OpenRead(@"https://api.vk.com/method/friends.get.xml?user_id=211344927&fields=nickname,photo_200_orig"))
using (var reader = new StreamReader(st, Encoding.UTF8))
users = (Users)new XmlSerializer(typeof(Users)).Deserialize(reader);
users.Items.AsParallel().ForAll(user => user.Init());
AutoScrollMinSize = new Size(200, ItemHeight * users.Items.Length);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
for(int i=0;i<users.Items.Length;i++)
{
var user = users.Items[i];
var y = ItemHeight * i - VerticalScroll.Value;
if (user.Photo != null)
e.Graphics.DrawImage(user.Photo, 0, y, user.Photo.Width * ItemHeight / user.Photo.Height, ItemHeight);
e.Graphics.DrawString(user.Uid.ToString(), Font, Brushes.Black, ItemHeight + 10, y);
e.Graphics.DrawString(user.FirstName, Font, Brushes.Black, ItemHeight + 10, y + Font.Height * 1);
e.Graphics.DrawString(user.LastName, Font, Brushes.Black, ItemHeight + 10, y + Font.Height * 2);
e.Graphics.DrawString(user.PhotoUrl, Font, Brushes.Black, ItemHeight + 10, y + Font.Height * 3);
}
}
}
[Serializable]
public class User
{
[XmlElement("uid")]
public string Uid { get; set; }
[XmlElement("first_name")]
public string FirstName { get; set; }
[XmlElement("last_name")]
public string LastName { get; set; }
[XmlElement("nickname")]
public string Nickname { get; set; }
[XmlElement("photo_200_orig")]
public string PhotoUrl { get; set; }
public Image Photo { get; set; }
public void Init()
{
if(!string.IsNullOrEmpty(PhotoUrl))
try
{
using (var st = new WebClient().OpenRead(PhotoUrl))
Photo = Image.FromStream(st);
}catch
{
}
}
}
[Serializable]
[XmlRoot("response")]
public class Users
{
[XmlElementAttribute("user")]
public User[] Items { get; set; }
}
}