简体   繁体   中英

Order different custom classes with XmlSerializer in same node

I have the following XML structure:

<TabHierarchy>
    <Tab Name="Tab1"/>
    <TabGroup Name="Group1">                
        <Tab Name="Tab2"/>
        <Tab Name="Tab3"/>
        <Tab Name="Tab4"/>
    </TabGroup>
    <Tab Name="Tab5"/>
    <TabGroup Name="Group2">
        <Tab Name="Tab6"/>
        <TabGroup Name="Group3">
            <Tab Name="Tab7"/>
            <Tab Name="Tab8"/>
        </TabGroup>
        <Tab Name="Tab9"/>
    </TabGroup>
    <Tab Name="Tab10"/>
</TabHierarchy>

My C# classes:

public class XmlTabHierarchy
{
    public XmlTabHierarchy()
    {
        Tab = new List<XmlTab>();
        TabGroup = new List<XmlTabGroup>();
        TabReport = new List<XmlReportTab>();
    }
    [XmlElement("Tab")] public List<XmlTab> Tab { get; set; }
    [XmlElement("TabGroup")] public List<XmlTabGroup> TabGroup { get; set; }
    [XmlElement("ReportTabs")] public List<XmlReportTab> TabReport { get; set; }
}
public class XmlTab
{
    [XmlIgnore] public int Order { get; set; }
    [XmlIgnore] public string Group { get; set; }
    [XmlIgnore] public bool Use { get; set; }
    [XmlIgnore] public bool ByUser { get; set; }
    [XmlIgnore] public bool KeyTab { get; set; }
    [XmlIgnore] public string Tab { get; set; }
    [XmlAttribute("Name")] public string TabClean { get; set; }
    [XmlAttribute("Alias")] public string Alias { get; set; }
    [XmlIgnore] public string Type { get; set; }
    [XmlIgnore] public bool Duplicate { get; set; }
}
public class XmlTabGroup
{
    public XmlTabGroup()
    {
        Tab = new List<XmlTab>();
        TabGroup = new List<XmlTabGroup>();
    }
    [XmlIgnore] public int Order { get; set; }
    [XmlIgnore] public int GroupId { get; set; }
    [XmlAttribute("Name")] public string Group { get; set; }
    [XmlAttribute("Alias")] public string Alias { get; set; }
    [XmlElement("Tab")] public List<XmlTab> Tab { get; set; }
    [XmlElement("TabGroup")] public List<XmlTabGroup> TabGroup { get; set; }
}

The issue I am having is, being able to sort them correctly. As seen in the example XML, Tabs can be between TabGroups. The ReportTabs are always at the end. There is one post here with a similar problem but I don't know how to implement it for my project. Keep sort when deserialize and serialize XML using XmlSerializer

XML specification the order of elements are not required. The code below creates the XML according to the XML specification.

using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApp2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            XmlTabHierarchy hierarchy = new XmlTabHierarchy()
            {
                Tab = new List<XmlTab>()
                {
                    new XmlTab() { TabClean = "Tab1"},
                    new XmlTab() { TabClean = "Tab5"},
                    new XmlTab() { TabClean = "Tab10"}
                },
                TabGroup = new List<XmlTabGroup>()
                {
                    new XmlTabGroup()
                    {
                        Group = "Group1",
                        Tab = new List<XmlTab>()
                        {
                            new XmlTab() { TabClean = "Tab2"},
                            new XmlTab() { TabClean = "Tab3"},
                            new XmlTab() { TabClean = "Tab4"}
                        }
                    },
                    new XmlTabGroup()
                    {
                        Group = "Group2",
                        Tab = new List<XmlTab>()
                        {
                            new XmlTab() { TabClean = "6" },
                            new XmlTab() { TabClean = "9"},
                        },
                        TabGroup = new List<XmlTabGroup>()
                        {
                            new XmlTabGroup()
                            {
                                Group = "Group3",
                                Tab = new List<XmlTab>()
                                {
                                    new XmlTab() { TabClean = "Tab7"},
                                    new XmlTab() { TabClean = "Tab8"}
                                }
                            }
                        }
                    }
                }
                
            };

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(XmlTabHierarchy));
            serializer.Serialize(writer, hierarchy);
            
        }

    }
    public class XmlTabHierarchy
    {
        public XmlTabHierarchy()
        {
            Tab = new List<XmlTab>();
            TabGroup = new List<XmlTabGroup>();
        }
        [XmlElement("Tab")] 
        public List<XmlTab> Tab { get; set; }
        [XmlElement("TabGroup")] 
        public List<XmlTabGroup> TabGroup { get; set; }
    }
    public class XmlTab
    {
        [XmlAttribute("Name")] 
        public string TabClean { get; set; }
        [XmlAttribute("Alias")] 
        public string Alias { get; set; }
    }
    public class XmlTabGroup
    {
        public XmlTabGroup()
        {
            Tab = new List<XmlTab>();
            TabGroup = new List<XmlTabGroup>();
        }
        [XmlAttribute("Name")] 
        public string Group { get; set; }
        [XmlAttribute("Alias")] 
        public string Alias { get; set; }
        [XmlElement("Tab")] 
        public List<XmlTab> Tab { get; set; }
        [XmlElement("TabGroup")] 
        public List<XmlTabGroup> TabGroup { get; set; }
    }


}
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM