Good day! I am trying to deserialize an XML document, but for some reason it turns out to select only the first element from XML.

class Program { static void Main(string[] args) { XmlSerializer formatter = new XmlSerializer(typeof(XmlClass)); using (FileStream fs = new FileStream(@"F:\XML1.xml", FileMode.Open)) { XmlClass newPerson = (XmlClass)formatter.Deserialize(fs); Console.WriteLine("Объект десериализован"); foreach (var o in newPerson.Info.Man) { Console.WriteLine("Значение: {0} ", o.info); Console.ReadKey(); } } } } 

Here is the class in which I pass the elements:

 [Serializable] [XmlRoot(ElementName = ("Document"))] public class XmlClass { [XmlElement(ElementName = "Personal")] public InfoClass Info { get; set; } } public class InfoClass { [XmlElement(ElementName = "Work")] public List<ManClass> Man { get; set; } } public class ManClass { [XmlAttribute(AttributeName = "info")] public string info { get; set; } } 

Sample XML document:

 <?xml version="1.0" encoding="utf-8" ?> <Document> <Personal> <Work info="Работающий"> <users info="1"> <user name="Bill Gates"> <company>Microsoft</company> <age>48</age> </user info="2"> <user name="Larry Page"> <company>Google</company> <age>42</age> </user> </users> </Work> </Personal> </Document> 
  • 3
    Give an example of XML. - iluxa1810
  • @Tibomso: What about XML? Something I do not believe that you have the XML tag <Главный элемент> . - VladD September
  • It seems I understood what was wrong, I wanted to select the "info" attribute from the "users" element, but it turned out that I filled the collection with the "info" attribute from the "work" element, but now the question is how do I fill the collection with the "users" attributes "... (: - Tibomso
  • Everything turned out well, but if you have any options I would like to look at them, because I think I didn’t do quite right (: - Tibomso

2 answers 2

Your ManClass class corresponds to the <Work> - and, by name, must match <users> or <user> .

Add a few more levels of classes.

    @Pavel Mayorov yes, I did, but thanks for the advice! (:

    Disarming:

     var formatter = new XmlSerializer(typeof(DocumentClass)); using (FileStream fs = new FileStream(@"F:\XML2.xml", FileMode.Open)) { var newPerson = (DocumentClass)formatter.Deserialize(fs); Console.WriteLine("Объект десериализован"); foreach (Users o in newPerson.Personal.work.users) { Console.WriteLine("Имя: {0}", o.user[0].name); Console.WriteLine("Значение: {0} {1}", o.user[0].company, o.user[0].age); Console.WriteLine("Имя: {0}", o.user[1].name); Console.WriteLine("Значение: {0} {1}", o.user[1].company, o.user[1].age); } Console.ReadKey(); } 

    Class:

     [Serializable] [XmlRoot(ElementName = ("Document"))] public class DocumentClass { [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; } }