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?