【问题标题】:Serialize Dotnet Collection without Root Element?序列化没有根元素的 dotnet 集合?
【发布时间】:2014-03-24 10:41:27
【问题描述】:

考虑以下序列化的xml:

<Course>
  <Title>Engineering</Title>
  <Student>
    <Name>John Doe</Name>
  </Student>
  <Student>
    <Name>Jane Doe</Name>
  </Student>
   ...
</Course>

很遗憾,我无法修改架构。 (理想情况下,我应该将 Student 类包装到 Students 根元素中!)

如何定义实体类以使序列化能够正常工作?

我尝试了以下代码,但这会生成带有 Students 作为包装器元素的 xml。

public class Course
{
    public string Title { get; set; }
    public List<Student> Students { get; set; }
}

public class Student
{
    public string Name { get; set; }
}

【问题讨论】:

    标签: c# xml wcf serialization xsd


    【解决方案1】:

    XmlElement 属性添加到您的Students 列表中,如下所示:

    public class Course
    {
        public string Title { get; set; }
    
        [XmlElement("Student")]
        public List<Student> Students { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-31
      • 2014-10-06
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      相关资源
      最近更新 更多