I'm trying to parse a simple xml file

-<root> -<content> -<category title="Затворы дисковые" icon="zatvor_diskovyi" subtitle="УК 99068, УК 99025 (PN 2,5 МПа)"> <action title="Общие данные" screenUrl="zatvor_disk_specification.xml" type="Specification"/> <action title="Конструкция" screenUrl="zatvor_disk_rotator.xml" type="Rotator"/> <action title="Принцип действия" screenUrl="zatvor_disk_princip_deistviya.xml" type="Video"/> <action title="Чертёж общего вида" screenUrl="zatvor_disk_chertej.xml" type="TextImageView"/> </category> -<category title="Затворы какие-то еще" icon="another_zatvor" subtitle="УК 432428, УК 32143 (PN 23,5 МПа)"> <action title="Same_Общие данные" screenUrl="Same_zatvor_disk_specification.xml" type="Same_Specification"/> <action title="Same_Конструкция" screenUrl="Same_zatvor_disk_rotator.xml" type="Same_Rotator"/> <action title="Same_Принцип действия" screenUrl="Same_zatvor_disk_princip_deistviya.xml" type="Same_Video"/> <action title="Same_Чертёж общего вида" screenUrl="Same_zatvor_disk_chertej.xml" type="Same_TextImageView"/> </category> </content> </root> 

Accordingly made 3 classes:

Action

 public class action { [XmlAttribute("type")] public String type; [XmlAttribute("title")] public String title; [XmlAttribute("screenUr1")] public String screenUrl; } 

Category

 public class category { [XmlArray("category")] [XmlArrayItem("action")] public action[] category; [XmlAttribute("title")] public String title; [XmlAttribute("subtitle")] public String subtitle; [XmlAttribute("icon")] public String icon; } 

CategoryContainer

 [XmlRoot("root")] public class CategoryContainer { [XmlArray("content"), XmlArrayItem("category")] public category[] content; public static CategoryContainer load(string path) { XmlSerializer serializer = new XmlSerializer(typeof(CategoryContainer)); FileStream stream = new FileStream(path, FileMode.Open); return serializer.Deserialize(stream) as CategoryContainer; } } 

So far, for convenience, I throw everything in String. As you can see, it turns out that in the category class I also have an array called category. If you change the class name, then the following line in the CategoryContainer class stops working:

  [XmlArray("content"), XmlArrayItem("category")] public category[] content; 

If you change the name of the array, then the line in the category category stops working accordingly:

  [XmlArray("category")] [XmlArrayItem("action")] public action[] category; 

Are there any solutions, using XMLSerializer? If not, then how best to do?

  • What is the actual question? - Alexander Petrov
  • The question is, is it possible to make it so that when you change, for example, the name public action [] category to public action [] actionList, the data from the XML still falls there. - Simatsu Edgeworth

1 answer 1

It works for me like this:

 [XmlRoot("root")] public class CategoryContainer { [XmlArray("content")] [XmlArrayItem("category")] public Category[] Content { get; set; } } public class Category { [XmlElement("action")] // <---------- public Action[] ActionList { get; set; } [XmlAttribute] public string title { get; set; } [XmlAttribute] public string icon { get; set; } [XmlAttribute] public string subtitle { get; set; } } public class Action { [XmlAttribute] public string title { get; set; } [XmlAttribute] public string screenUrl { get; set; } [XmlAttribute] public string type { get; set; } } 

And in your place I would rename the attributes:

  [XmlAttribute("title")] public string Title { get; set; } 

and so on. And also wrap stream in using : using (var stream = File.OpenRead(path)) return ...


Tip: the easiest way is to get Visual Studio to generate the right classes using this trick: put the XML in the buffer, choose Edit → Paste Special → Paste XML as classes. A valid, albeit non-optimal set of classes will be created for you, which can already be simplified.