首页 > 解决方案 > 如何使用 xmlserializer 序列化此名称列表?

问题描述

获取此示例 XML:

<Teaching StudyPoint="0" Description="xx" OtherDiscussionVideo="True" VideoAfterStudentItem="False">
    <Theme>Test 1</Theme>
    <SourceMaterial>Material 1</SourceMaterial>
    <Names>
        <Name Class="2">a</Name>
        <Name Class="3">b</Name>
        <Name Class="1">b</Name>
    </Names>
</Teaching>

到目前为止,我已经设法为序列化创建了这个类:

namespace OutlookCalIFConsole.MWBData
{
    public class Teaching
    {
        [XmlAttribute]
        public int StudyPoint
        {
            get => _StudyPoint; set => _StudyPoint = value;
        }
        private int _StudyPoint;

        [XmlAttribute]
        public string Description
        {
            get => _Description; set => _Description = value;
        }
        private string _Description;

        [XmlAttribute]
        public bool OtherDiscussionVideo
        {
            get => _OtherDiscussionVideo; set => _OtherDiscussionVideo = value;
        }
        private bool _OtherDiscussionVideo;


        [XmlAttribute]
        public bool VideoAfterStudentItem
        {
            get => _VideoAfterStudentItem; set => _VideoAfterStudentItem = value;
        }
        private bool _VideoAfterStudentItem;

        public string Theme
        {
            get => _Theme; set => _Theme = value;
        }
        private string _Theme;

        public string SourceMaterial
        {
            get => _SourceMaterial; set => _SourceMaterial = value;
        }
        private string _SourceMaterial;

        public Teaching()
        {
            _OtherDiscussionVideo = false;
            _VideoAfterStudentItem = false;
            _StudyPoint = 0;
            _Description = "";
            _Theme = "";
            _SourceMaterial = "";
        }
    }
}

如何序列化Name值列表?

标签: c#xml-serialization

解决方案


尝试在Teaching课堂内进行以下操作:

public class Teaching {
    [XmlArrayItem("Name")]
    public Name[] Names;
}

推荐阅读