c#将对象类转换为XML

时间:2016-02-11 19:27:05

标签: c# xml linq

我对Class对象有点新的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或类似的方式转换这些Object,而不是使用每个类的streamwriter写入XML并创建更多&#34;手动&#34; 。希望这是可以解释的。
如果您需要更多关于我要问的问题的信息,请告诉我。

我也使用Visual Studio 2010 Professional。

1 个答案:

答案 0 :(得分:0)

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