【问题标题】:how to serialize a property property only not the whole property using xml attributes?如何使用 xml 属性仅序列化属性属性而不是整个属性?
【发布时间】:2019-05-07 18:08:45
【问题描述】:

我想序列化一个包含属性的对象,我只想序列化它的名称。

如何在不实现IXmlSerializable 接口的情况下做到这一点,只使用属性。

[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这个属性我不想忽略。

我只想序列化它的CurrentClass.ClassId 值。

【问题讨论】:

  • 你能告诉我们你想在 XML 中看到什么吗?

标签: c# xml-serialization xml-attribute ixmlserializable


【解决方案1】:

如果您只是在寻找与 Student 对象一起序列化的 ClassId,那么只拥有一个 ClassId 属性而不是整个 Class 对象更有意义。

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;

}

【讨论】:

  • 不,我想我不清楚。我想要的是序列化 Class 对象的所有属性,在学生对象上我想序列化它的所有属性 excpet currentClass 属性我不想序列化它我想序列化它的一个属性
  • 那么我的问题是,是否有理由需要 Student 类中的整个 CurrentClass 对象?如果您要查找的只是 ClassId,为什么不只是在 Student 类中有一个 ClassId 属性?由于您在当前班级对象中有一个学生列表,因此当您创建一个新学生时,您可以将 ClassId 传递给 Student 构造函数。
  • 我这样做是因为我想要整个班级,但是如果我序列化整个班级就会出现错误
  • 好的,但是您不能序列化位于 Class 对象列表中的 Student 对象,因为这会产生一种情况,即它本质上是学生和班级的永无止境的循环。你必须要么让学生有一个班级对象,要么让一个班级有一个学生对象列表,但不能两者兼而有之。简单的方法是在 Student 类中包含 ClassId 和 Teacher 属性,并在初始化 student 时分配它们。我将编辑我的代码示例以向您展示我的意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 2023-04-07
  • 2020-04-28
相关资源
最近更新 更多