将 c# 对象类转换为 XML

本文关键字:XML 转换 对象 | 更新日期: 2023-09-27 18:34:11

我对将类对象转换为XML文件有点陌生。
我有一系列课程
课堂:

class ClassRoom
{
    private string m_sName = string.Empty;
    public string Name
    {
        get { return m_sName; }
        set { m_sName = value; }
    }
    private List<Student> students = null;
    public List<Student> Students
    {
        get { return students == null ? students = new List<Student>() : students; }
        set { students = value; }
    }
    private Teacher teacher = null;
    public Teacher TeaccherforClassroom
    {
        get { return teacher == null ? teacher = new Teacher() : teacher; }
        set { teacher = value; }
    }
    private Teacher subTeacher = null;
    public Teacher SubTeaccherforClassroom
    {
        get { return subTeacher == null ? subTeacher = new Teacher() : subTeacher; }
        set { subTeacher = value; }
    }
    internal class Student
    {
        private string name = string.Empty;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
    internal class Teacher
    {
        private string name = string.Empty;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}

也人

    class People
{
    private List<Person> person = null;
    public List<Person> Persons
    {
        get { return person == null ? person = new List<Person>() : person; }
        set { person = value; }
    }
    internal class Person
    {
        private string name = string.Empty;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int age = 0;
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }
}

我需要这样的东西来转换为这个XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Class roomNumber="201" date="JANUARY 31, 2016">
  <ClassRoom Name="SCI101">
    <Students Name="Tom">
        <StudentNumber>545454</FundRiskMeasure>
    </Students>
    <Students Name="Jerry">
        <StudentNumber>123456</FundRiskMeasure>
    </Students>
    <TeaccherforClassroom>
        <Name>"Smith"</Name>
    </TeaccherforClassroom>
    <SubTeaccherforClassroom>
        <Name>"Johnny"</Name>
    </SubTeaccherforClassroom>
  </ClassRoom>
  <People>
    <Persons Series="Peter">
      <Age>22</Age>
    </Persons>
    <Persons Series="Jenny">
      <Age>21</Age>
    </Persons>
    <Persons Series="John">
      <Age>24</Age>
    </Persons>
  </People>
</Class>

我研究了 XML 可序列化,但似乎我的问题更具挑战性,因为我可以在类中列出新对象。例如,教室有一个学生列表及其属性。
我也有名称在节点<Students Name="Tom">中弹出,或者它可能会作为属性弹出

<TeaccherforClassroom>
    <Name>"Smith"</Name>
</TeaccherforClassroom>


我只是想知道是否有一种简单的方法可以使用 LINQ 或类似方式转换这些对象,而不是为每个类使用流编写器写入 XML 并更"手动"地创建它。希望这是可以解释的。
如果您需要有关我所问问题的更多信息,请告诉我。

我也在使用Visual Studio 2010 Professional。

将 c# 对象类转换为 XML

你的类的所有成员都是静态的,据我所知,没有序列化程序可以序列化静态类/静态属性。也许您应该重新考虑静态修饰符。作为一个简单的 1 行解决方案,您可以使用 System.Xml.Serialization.XmlSerializer 进行简单的序列化/反序列化(对于非静态类/属性)。