Good day! Trying to select the value of the XML file corresponding to the value in the DataGridView. It turned out to put the value in the DataGrid and select a value, BUT the choice is made regardless of the row, that is, the value will be one regardless of which line to click.
Class:
[Serializable] [XmlRoot(ElementName = ("Document"))] public class Document { [XmlElement(ElementName = "Personal")] public Personal Personal { get; set; } } public class Personal { [XmlElement(ElementName = "Work")] public Work work { get; set; } [XmlAttribute(AttributeName = "info")] public string info { get; set; } } public class Work { [XmlElement(ElementName = "users")] public List<Users> users { get; set; } [XmlAttribute(AttributeName = "info")] public string info { get; set; } } public class Users { [XmlAttribute(AttributeName = "info")] public string info { get; set; } [XmlElement(ElementName = "user")] public List<User> user { get; set; } } public class User { [XmlAttribute(AttributeName = "name")] public string name { get; set; } [XmlElement(ElementName = "company")] public string company { get; set; } [XmlElement(ElementName = "age")] public string age { get; set; } } Code:
public static XmlSerializer format = new XmlSerializer(typeof(Document)); public static FileStream read = new FileStream(@"F:\XML2.xml", FileMode.Open); Document XmlDes = (Document)format.Deserialize(read); //По нажатию кнопки, заполняем DataGrid private void button1_Click(object sender, EventArgs e) { foreach (var poisk in XmlDes.Personal.work.users) { userBindingSource.DataSource = poisk.user; } } //При нажатии на любую строку в DataGrid получаем значение private void userData_CellContentClick(object sender, DataGridViewCellEventArgs e) { foreach (var poisk in XmlDes.Personal.work.users) { //Вот тут загвоздка, я понимаю как вывести конкретный элемент, но как сделать, так что бы он соответствовал значению строки? CollectionUser.Rows.Add(poisk.user[0].age, poisk.user[0].name); } } XML file:
<?xml version="1.0" encoding="utf-8" ?> <Document> <Personal> <Work info="Работающий"> <users info="Информация о работниках"> <user name="Bill Gates"> <company>Microsoft</company> <age>48</age> </user> <user name="Larry Page"> <company>Google</company> <age>42</age> </user> </users> </Work> </Personal> </Document> Example: In the DataGrid, when you click on a button, an attribute (name) and two elements (company & age) appear. The second line displays the values of the second element (Larry).