反序列化 XML 个具有不同标签的相同项目

Deserialize XML same items with different tag

这是一个 XML 示例,我想反序列化并在桌面应用程序中使用,用户可以在其中插入一个 XML,如下所示:

   <Settings>
        <FileName>C:\Users\myniceuser\Desktop\hello.xmldoc</FileName>
        <Zones>
                <Row1>
                        <Address>2</Address>strong text
                        <Zone>2</Zone>
                        <Installed>True</Installed>
                </Row1>
                <Row2>
                        <Address>3</Address>
                        <Zone>2</Zone>
                        <Installed>True</Installed>
                </Row2>
                <Row3>
                        <Address>4</Address>
                        <Zone>2</Zone>
                        <Installed>True</Installed>
                </Row3>
                <Row4>
                        <Address>5</Address>
                        <Zone>2</Zone>
                        <Installed>True</Installed>
                </Row4>
        </Zones>
        <Network_IP>010.000.008.072</Network_IP>
        <Ethernet_Enable>true</Ethernet_Enable>
        <Language>1</Language>
</Settings>

我实现的class是这个:

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

namespace MyApp.Models
{
    [XmlRoot(ElementName = "Settings")]
    public class XML_Settings
    {

        [XmlElement(ElementName = "FileName")]
        public string FileName { get; set; }


        //[XmlElement(ElementName = "Zones")]
        [XmlElement(ElementName ="Zones")]
        public Zones_Points[] Zones_Points { get; set; }

        [XmlElement(ElementName = "Network_IP")]
        public string NetworkIP { get; set; }

        [XmlElement(ElementName = "Ethernet_Enable")]
        public bool EthernetEnable { get; set; }

        [XmlElement(ElementName = "Language")]
        public int Language { get; set; }
}
    [XmlRoot(ElementName = "")]
    public partial class Zones_Points
    {
        [XmlElement(ElementName ="Address")]
        public int Address { get; set; }

        [XmlElement(ElementName ="Zone")]
        public int PointZone { get; set; }

        [XmlElement(ElementName ="Installed")]
        public bool Installed { get; set; }

}
}

我这样反序列化:

Models.XML_Settings inputted_xml = new();
using StreamReader stream_xml = new StreamReader(xmlfile_path, CodePagesEncodingProvider.Instance.GetEncoding(1253));
                
XmlSerializer serializer = new XmlSerializer(typeof(Models.XML_Settings));

try
{
 Models.XML_Settings input = (Models.XML_GR6500)serializer.Deserialize(stream_xml);
 }
 catch (Exception exception)
 {
 MessageBox.Show(exception.Message);
 }

我希望我的应用像这样在 XML 中输入文件,并将它们的数据转换为我可以使用的 class。

反序列化创建的对象“输入”使每个数据都是正确的,除了 XML 中具有“行”标签的区域。这些“行”每次都可能多于或少于 4。

如何将带有行标签的项目反序列化并输入到 Zones_Points 列表或数组中?

使用 IXmlSerializable

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

namespace ConsoleApplication2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(XML_Settings));
            XML_Settings settings = (XML_Settings)serializer.Deserialize(reader);
  
        }
    }
    [XmlRoot("Settings")]
    public class XML_Settings
    {


        [XmlElement(ElementName = "FileName")]
        public string FileName { get; set; }

        [XmlElement(ElementName = "Zones")]
        public Zones_Points Zones_Point { get; set; }
        [XmlElement(ElementName = "Network_IP")]
        public string NetworkIP { get; set; }
        [XmlElement(ElementName = "Ethernet_Enable")]
        public bool EthernetEnable { get; set; }
        [XmlElement(ElementName = "Language")]
        public int Language { get; set; }
    }
    public partial class Zones_Points : IXmlSerializable
    {
        // Xml Serialization Infrastructure
        [XmlElement(ElementName = "Zones")]
        public List<Zones_Point> Points { get; set; }

        public void WriteXml(XmlWriter writer)
        {
        }

        public void ReadXml(XmlReader reader)
        {

            XElement zones = (XElement)XElement.ReadFrom(reader);
            Points = new List<Zones_Point>();
            foreach (XElement row in zones.Elements())
            {
                Zones_Point Point = new Zones_Point();
                Points.Add(Point);
                Point.Address = (int)row.Element("Address");
                Point.PointZone = (int)row.Element("Zone");
                Point.Installed = (Boolean)row.Element("Installed");
            }

        }

        public XmlSchema GetSchema()
        {
            return (null);
        }
    }
    public partial class Zones_Point
    {
        [XmlElement(ElementName = "Address")]
        public int Address { get; set; }
        [XmlElement(ElementName = "Zone")]
        public int PointZone { get; set; }
        [XmlElement(ElementName = "Installed")]
        public bool Installed { get; set; }
    }
}