Tell me. All I have is this:

public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void XML_Click(object sender, EventArgs e) { } 

xml such

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <graph> <title>Dijkstra</title> <points> <point id="1" y="3" x="1"/> </points> <lines> <line id="1" weight="10" to="2" from="1"/> </lines> </graph> 
  • Example xml file show. - Bulson
  • <? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> <graph> <title> Dijkstra </ title> <points> <point id = "1" y = "3" x = "1" /> </ points> <lines> <line id = "1" weight = "10" to = "2" from = "1" /> </ lines> </ graph> - Andrew Korpach
  • And what have you failed? Did you read any literature at all? Documentation? XML is so trivial. - Andrey NOP
  • one
    Well, in the class structure , not in the console. Then this class structure can be used even in the console application, even in WinForms, even in the web. - Andrei NOP
  • one
    Ways to read xml - Alexander Petrov

1 answer 1

For such a file

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <graph> <title>Dijkstra</title> <points> <point id="1" y="3" x="1"/> <point id="2" y="30" x="10"/> <point id="3" y="300" x="100"/> </points> <lines> <line id="1" weight="10" to="2" from="1"/> <line id="2" weight="100" to="20" from="10"/> <line id="3" weight="1000" to="200" from="100"/> </lines> 

it turned out

Work example

Go here and get such a set of classes.

 namespace WinFormsAppXmlParse { [XmlRoot(ElementName = "point")] public class Point { [XmlAttribute(AttributeName = "id")] public string Id { get; set; } [XmlAttribute(AttributeName = "y")] public string Y { get; set; } [XmlAttribute(AttributeName = "x")] public string X { get; set; } } [XmlRoot(ElementName = "points")] public class Points { [XmlElement(ElementName = "point")] public List<Point> Point { get; set; } } [XmlRoot(ElementName = "line")] public class Line { [XmlAttribute(AttributeName = "id")] public string Id { get; set; } [XmlAttribute(AttributeName = "weight")] public string Weight { get; set; } [XmlAttribute(AttributeName = "to")] public string To { get; set; } [XmlAttribute(AttributeName = "from")] public string From { get; set; } } [XmlRoot(ElementName = "lines")] public class Lines { [XmlElement(ElementName = "line")] public List<Line> Line { get; set; } } [XmlRoot(ElementName = "graph")] public class Graph { [XmlElement(ElementName = "title")] public string Title { get; set; } [XmlElement(ElementName = "points")] public Points Points { get; set; } [XmlElement(ElementName = "lines")] public Lines Lines { get; set; } } } 

We will work in the spirit of MVP , here is the interface and the codecichind of the form that it implements

 public interface IMainForm { string Output { get; set; } event EventHandler<string> SelectedFile; event EventHandler<string> SelectedNode; } public partial class MainForm : Form, IMainForm { public MainForm() { InitializeComponent(); } public string Output { get => textBoxOutput.Text; set => textBoxOutput.Text = value; } //событие выбора файла public event EventHandler<string> SelectedFile; //событие выбора в listbox public event EventHandler<string> SelectedNode; //выбор файла private void buttonSelectFile_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); System.IO.FileInfo example = new System.IO.FileInfo("file.xml"); openFileDialog.CheckFileExists = true; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); openFileDialog.Filter = string.Format("{0} файлы ({1})|*{1}|Все файлы (*.*)|*.*", example.Extension.Substring(1).ToUpper(), example.Extension); var result = openFileDialog.ShowDialog(); if (result == DialogResult.OK) { textBoxFile.Text = openFileDialog.FileName; textBoxOutput.Text = String.Empty; //вызываем событие выбора файла SelectedFile?.Invoke(this, openFileDialog.FileName); } else { textBoxFile.Text = String.Empty; textBoxOutput.Text = String.Empty; } } //выбор раздела private void listBoxSelectNode_SelectedValueChanged(object sender, EventArgs e) { var item = listBoxSelectNode.SelectedItem.ToString(); //вызываем событие выбора в listbox SelectedNode?.Invoke(this, item); } } 

Here is the Presenter class, in which we will work with xml

 public class Presenter { private readonly IMainForm _mainForm; private Graph _graph; //ctor public Presenter(IMainForm mainForm) { _mainForm = mainForm ?? throw new ArgumentNullException(nameof(mainForm)); _mainForm.SelectedFile += _mainForm_SelectedFile; _mainForm.SelectedNode += _mainForm_SelectedNode; } /// <summary> /// Выбран файл /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _mainForm_SelectedFile(object sender, string file) { if (String.IsNullOrEmpty(file)) throw new ArgumentNullException(file); XmlSerializer serializer = new XmlSerializer(typeof(Graph)); // читаем файл и десериализуем using (StreamReader sr = new StreamReader(file)) { _graph = (Graph)serializer.Deserialize(sr); } } /// <summary> /// Выбрана нода /// </summary> /// <param name="sender"></param> /// <param name="item"></param> private void _mainForm_SelectedNode(object sender, string item) { if (String.IsNullOrEmpty(item)) throw new ArgumentNullException(item); if (_graph == null) return; switch (item) { case "title": _mainForm.Output = _graph.Title; break; case "points": _mainForm.Output = ShowPoints(_graph.Points); break; case "lines": _mainForm.Output = ShowLines(_graph.Lines); break; default: throw new ArgumentException(nameof(item)); } } /// <summary> /// Формирование вывода Точек /// </summary> /// <param name="points"></param> /// <returns></returns> private string ShowPoints(Points points) { StringBuilder sb = new StringBuilder(); foreach (var point in points.Point) { var str = $"Точка:{point.Id} - X:{point.X}, Y:{point.Y}"; sb.AppendLine(str); } return sb.ToString(); } /// <summary> /// Формирование вывода Линий /// </summary> /// <param name="lines"></param> /// <returns></returns> private string ShowLines(Lines lines) { StringBuilder sb = new StringBuilder(); foreach (var line in lines.Line) { var str = $"Линия:{line.Id} - начало:{line.From}, конец:{line.To}, толщина:{line.Weight}"; sb.AppendLine(str); } return sb.ToString(); } } 

Presenter and View (form) will be linked together in Program.cs so

 [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MainForm form = new MainForm(); Presenter presenter = new Presenter(form); Application.Run(form); } 
  • Thank you very much, they helped out 100% straight - Andrew Korpach
  • Can I link to the archive with your work? - Andrew Korpach
  • @KonstantinVoskov, here you are. - Bulson