Search code examples
c#xml-serializationxml-attributeixmlserializable

how to serialize a property property only not the whole property using xml attributes?


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.


Solution

  • 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;
    
    }