1

I want to serialize an object that contains properties and one of these properties I want only to serialize its name.

How to do this without implmeneting IXmlSerializable interface, only using attributes.

[Serializable]
public class Class
{
    public Class()
    {
    }

    [XmlAttribute]
    public string ClassId{get;set;}

    [XmlAttribute]
    public Teacher Teacher{get;set;}

    [XmlArray("Students")]
    [XmlArrayItem("Student", Type=typeof(Student))]
    public List<Student> Students { get; } = new List<Student>();
}


[Serializable]
public class Student 
{
    public Student()
    {

    }

    public Class CurrentClass{get;set;}


    [XmlAttribute]
    public string Name { get; set; } = "New Student";

    [XmlAttribute]
    public int Age { get; set; } = 10;

}

CurrentClass this property I don't want to ignore it.

I want to serialize only its CurrentClass.ClassId value.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Shehab
  • 431
  • 2
  • 10

1 Answers1

0

If you are looking just for the ClassId to be serialized with the Student object it makes more sense to just have a ClassId property rather than the entire Class object.

public class GenerateXml
{
        public static void Create()
        {
            Class c = new Class();
            c.Teacher = new Teacher() {Name = "Mr. Henry"};
            var s = new Student() { Age = 14, Name = "Suzy", Teacher = c.Teacher };
            c.Students.Add(s);
            s = new Student() {Age = 13, Name = "Adam", Teacher = c.Teacher};
            c.Students.Add(s);

            var serializer = new XmlSerializer(c.GetType());
            XmlTextWriter writer = new XmlTextWriter("class.xml", Encoding.ASCII);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;
            serializer.Serialize(writer, c);
        }
}

[Serializable]
public class Class
{
    public Class()
    {
    }

    [XmlAttribute]
    public string ClassId { get; set; }

    [XmlElement]
    public Teacher Teacher { get; set; }

    [XmlArray("Students")]
    [XmlArrayItem("Student", Type = typeof(Student))]
    public List<Student> Students { get; } = new List<Student>();
}


[Serializable]
public class Student
{
    public Student()
    {

    }

    [XmlElement]
    public Teacher Teacher { get; set; }

    [XmlAttribute]
    public string ClassId { get; set; }

    [XmlAttribute]
    public string Name { get; set; } = "New Student";

    [XmlAttribute]
    public int Age { get; set; } = 10;

}

[Serializable]
public class Teacher
{
    public Teacher()
    {

    }

    [XmlAttribute]
    public string Name { get; set; } = "New Teacher";

    [XmlAttribute]
    public int Age { get; set; } = 30;

}
Versex
  • 54
  • 5
  • No I think i was not clear. what I want is to serialize all the properties of Class object and on the student object I want to serialize all of its properties excpet currentClass property I don't want to serialize it I want to serialize one of its properties – Shehab May 07 '19 at 18:32
  • So my question then would be, is there a reason that you need the entire CurrentClass object in the Student class? If all you are looking for is the ClassId, why do you not just have a ClassId property in the Student class? Since you have a list of students in the current class object, when you create a new student you could pass the ClassId into the Student constructor. – Versex May 07 '19 at 18:43
  • I'm doing this because I want the whole class but if I serialize the whole class an error will occur – Shehab May 07 '19 at 19:21
  • Ok, but you cannot serialize a Student object that is in a list in the Class object because this creates a situation where it is essentially a never ending cycle of student and class. You have to either have the student have a class object or a class have a list of student object, but not both. The simple approach would be to include a ClassId and Teacher properties in the Student class and assign them when you initialize student. I will edit my code sample to show you what I mean. – Versex May 07 '19 at 20:17