public class Stroka
{
public void Main(string el1, string el2, string el3, string el4)
{
El1 = el1;
El2 = el2;
El3 = el3;
El4 = el4;
}
public string El1 { get; set; }
public string El2 { get; set; }
public string El3 { get; set; }
public string El4 { get; set; }
public void SaveToFile(XmlTextWriter xmlOut)
{
xmlOut.WriteStartElement("Строка");
xmlOut.WriteAttributeString("Колонка1", El1.ToString());
xmlOut.WriteAttributeString("Колонка2", El2.ToString());
xmlOut.WriteAttributeString("Колонка3", El3.ToString());
xmlOut.WriteAttributeString("Колонка4", El4.ToString());
xmlOut.WriteEndElement();
}
public void LoadFromFile(XmlTextReader xmlin)
{
try
{
El1 = xmlin.GetAttribute("Колонка1");
El2 = xmlin.GetAttribute("Колонка2");
El3 = xmlin.GetAttribute("Колонка3");
El4 = xmlin.GetAttribute("Колонка4");
}
catch(Exception)
{ }
}
}
using System.IO;
using System.Xml;
private void экспорттелефоннойКнигиToolStripMenuItem_Click(object sender, EventArgs e)
{
int count = listView1.Items.Count;
if (count == 0)
{
MessageBox.Show("Список Пуст. Нечего экспортировать.");
return;
}
if (filename == "")
{
SaveFileDialog fileDialog = new SaveFileDialog();
fileDialog.Title = "Экспорт";
fileDialog.Filter = "XML files|*.xml";
if (fileDialog.ShowDialog() != DialogResult.OK)
return;
filename = fileDialog.FileName;
Export();
}
}
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.IO;
using System.Xml;
namespace TestPhoneBook
{
public partial class MainWindow : Form
{
string filename = "";
public MainWindow()
{
InitializeComponent();
}
...
public void Export()
{
//создание потока записи и объекта создания xml-документа
FileStream fs = new FileStream(filename, FileMode.Create);
XmlTextWriter xmlOut = new XmlTextWriter(fs, Encoding.Unicode);
xmlOut.Formatting = Formatting.Indented;
//старт начала документа
xmlOut.WriteStartDocument();
xmlOut.WriteComment("Тестовое задание. Телефонная книга");
xmlOut.WriteComment("Работу выполнил: Григорий Чуприна");
//создание корневого документа
xmlOut.WriteStartElement("PhoneBook");
xmlOut.WriteAttributeString("Version", "1.0.0");
//цикл перебора всех элементов
int count = listView1.Items.Count;
for (int t = 0; t < count; t++)
{
ListViewItem item = listView1.Items[t];
Stroka stroka = new Stroka();
stroka.Main(item.SubItems[0].Text, item.SubItems[1].Text, item.SubItems[2].Text, item.SubItems[3].Text);
stroka.SaveToFile(xmlOut);
}
//закрытие основного тега и документа
xmlOut.WriteEndElement();
xmlOut.WriteEndDocument();
//закрытие объекта записи
xmlOut.Close();
fs.Close();
}
Stroka stroka = new Stroka();
stroka.Main(item.SubItems[0].Text, item.SubItems[1].Text, item.SubItems[2].Text, item.SubItems[3].Text);
К сожалению, не доступен сервер mySQL