0

I am a beginner in XML and I would like to save Students with their fields into XML. But I dont know how to save "type" as object. Here is my code:

class Student
{
    private int id;
    private string name;
    private string surname;
    public Type type;    

    public Student(int id, string name, string surname, Type type)
    {
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.type = type;
    }
}

class Type
{
    private string ID;
    private string name;

    public Odbor(string ID, string name)
    {
       this.ID = ID;
       this.name= name;
    }
}

class Program
{
    static void Main(string[] args)
    { 
        Type type1 = new Type("IRT","info");
        Type type2 = new Type("MATH","mathematic");
        List<Student> Students = new List<Student>();
        Students.Add(new Student(10,"Jon","Snow", type1));
        Students.Add(new Student(11,"Ned","Stark", type2));
        //Save Students to XML
    }
}

Thanks for help.

Deyeth
  • 59
  • 2
  • 11
  • 2
    Possible duplicate of [How to serialize a List into XML?](http://stackoverflow.com/questions/17043663/how-to-serialize-a-listt-into-xml) – techspider May 10 '16 at 18:36
  • 1
    http://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part – user853710 May 10 '16 at 18:45
  • 1
    See [Serialize an object to XML](https://stackoverflow.com/questions/4123590/serialize-an-object-to-xml) and [How to serialize/deserialize simple classes to XML and back](https://stackoverflow.com/questions/3356976/how-to-serialize-deserialize-simple-classes-to-xml-and-back). Note that `XmlSerializer` only serializes `public` classes and members. – dbc May 10 '16 at 18:57

1 Answers1

0

You can try something like this : Note This code is for example purposes only and can be made much better

using System.Xml.Linq;

    class Student { 


            public int id;
            public string name;
            public string surname;
            public Type type;

            public Student(int id, string name, string surname, Type type)
            {
                this.id = id;
                this.name = name;
                this.surname = surname;
                this.type = type;
            }
        }


        class Type
        {
            public string ID;
            public string name;

            public Type(string ID, string name)
            {
                this.ID = ID;
                this.name = name;
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                Type type1 = new Type("IRT", "info");
                Type type2 = new Type("MATH", "mathematic");
                List<Student> Students = new List<Student>();
                Students.Add(new Student(10, "Jon", "Snow", type1));
                Students.Add(new Student(11, "Ned", "Stark", type2));
                //Save Students to XML


                XElement xmlElements = new XElement("Students");
                foreach (Student student in Students)
                {
                    var xmlstudent     = new XElement("student");
                    var xmlstudenttype = new XElement("studenttype");

                    xmlstudenttype.Add(new XAttribute("ID", student.type.ID),
                                       new XAttribute("name",student.type.name));
                    xmlstudent.Add(new XAttribute("id", student.id),
                                   new XAttribute("name", student.name),
                                   new XAttribute("surname", student.surname), 
                                   new XElement("type", xmlstudenttype));

                    xmlElements.Add(xmlstudent);
                }

                System.Console.Write(xmlElements);
                System.Console.Read();
            }
        }