【问题标题】:XML Serialization Parent ElementsXML 序列化父元素
【发布时间】:2011-04-08 13:59:11
【问题描述】:

我必须将应用程序中的数据序列化为 XML,但它必须符合现有格式,例如:

<Microsoft>
   <Office>
     <Students>
            <Student Name="Blah" />
      </Students>
   </Office>
</Microsoft>

或者类似的东西,Student 是我正在序列化的实体,我需要告诉它作为 Microsoft/Office 的子实体。有没有办法做到这一点?

谢谢。

【问题讨论】:

    标签: c# .net xml serialization


    【解决方案1】:

    您需要一个 Microsoft 对象和一个 Office 对象。

    例如:

    public class Microsoft
    {
        public IList<Office> Office {get; set;}
    }
    
    public class Office
    { 
        public Student CurrentStudent {get; set;}
    }
    

    这将反序列化为带有列表(1 个或多个)Office 对象的 MS 对象。每个 Office 节点将有 1 个且只有 1 个学生对象。

    【讨论】:

    • 谢谢,我只是考虑过这一点,但似乎应该有一种方法可以通过属性或其他方式来控制它?
    猜你喜欢
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多