There is XML that has a simple structure:

<Employee> <people> <Name>Василий</Name> <LastName>Петрович</LastName> </people> </Employee> 

And so on with a dozen entries.

There is a WPF form on which ListBox placed. When loading a form, the names of employees taken from <Name> are placed in the ListBox . How can you accomplish the task, that if a ListBox clicked on a name, then let's say its name is displayed in the neighboring Label ?

 //Загрузка элементов Name и помещение их в ListBox XElement xElements = XElement.Load(@"Employee.xml"); foreach (XElement xEle in xElements.Descendants("Name")) { Listbox_Employee.Items.Add((string)xEle); } 

Yes, this moment, this task works exactly half for me. That is, if I launch the program, add new users, and then select a new name in the ListBox , its last name will be shown. But, it will be shown exactly until the program is restarted, because the values ​​are stored in the RAM at run time. Accordingly, if after launching the program, I add new names, then after clicking on them the names will be shown, but if I click on the names that were added last time, nothing will happen.

I understand that theoretically, after clicking on the ListBox object, the method that opens XML should execute, execute the Container.Descendant s method and relate that it is this last name that applies to this ListBox object.
Thanks for the help, Amateur.
By the way, I work using XElement.

Addition 1:
Solving the problem on the recommendation of Bulson, I stumbled upon another problem. I created an even simpler XML structure:

 <MainData> <Name>Василий</Name> <LastName>Петрович</LastName> </MainData> 

Created a simple WPF form with TextBox and ListBox. Further, according to the recommendations, I created a separate class:

 [Serializable, XmlRoot("MainData")] public class DataExctract { [XmlAttribute("LastName")] public string LastName { get; set; } } 

Further, on the form:

 public MainWindow() { InitializeComponent(); LoadXml(); //Выполняем метод при загрузке окна } //Этот метод загружает XML и помещает дочерний элемент Name в ListBox public void LoadXml() { try { XElement xElements = XElement.Load(@"DataXML.xml"); foreach (XElement xEle in xElements.Descendants("Name")) { listBox.Items.Add((string)xEle); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } //При двойном щелчке по любому элементу помещаем фамилию в TextBox private void listBox_MouseDoubleClick(object sender, MouseButtonEventArgs e) { try { StreamReader reader = new StreamReader(@"DataXML.xml"); XmlSerializer serializer = new XmlSerializer(typeof(List<DataExctract>)); DataExctract DE = (DataExctract)serializer.Deserialize(reader); textBox.Text = DE.LastName; reader.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } 

As a result, I get an exception:

 There is an error in XML document (1, 2) 

This error appears when I double-click on a name. I found 2 similar questions in the English-speaking Stack, but I can not understand the root of evil (there is not enough knowledge of English).

  • one
    You do not need to add a visual-studio tag to every C # question. - andreycha
  • one
    OK I understood. - Amateur

1 answer 1

  1. You need to create a class Person with properties for name.
  2. Create a Repository class with methods for writing / reading an xml file.
  3. At the start, read the xml file in List<Person> People and assign it as a ListBox.ItemsSource .
  4. Create property public Person SelectedPerson { get; set; } public Person SelectedPerson { get; set; } public Person SelectedPerson { get; set; } and bind this property to ListBox.SelectedItem , then when you select from the list, you will have this property changed and you can extract the last name from it and show it on the Label . This description is in general terms, many more different nuances.
  • Thank you for your answer. I will come to work tomorrow and try to implement it :) - Amateur