.NET XML序列化数组/列表对象别名

4

我需要像这样序列化一个对象:

public class Book
{
  public string Title      { get; set; }
  public string[] Authors      { get; set; }
}

这会生成类似于这样的内容:
<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Good Book</Title>
  <Authors>
        <string>Author1</string>
        <string>Author2</string>
  </Authors>
</Book>

我正在寻找类似以下内容的东西:

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Good Book</Title>
  <Authors>
        <AuthorName>Author1</AuthorName>
        <AuthorName>Author2</AuthorName>
  </Authors>
</Book>

作者名只是一个字符串。我如何在不创建字符串包装器的情况下完成这个任务?
谢谢。
2个回答

2
使用XmlArrayItem属性:
public class Book
{
    public string Title { get; set; }

    [XmlArrayItem("AuthorName")]
    public string[] Authors { get; set; }
}

2
使用 XmlArrayItem 属性
public class Book
{
  public string Title { get; set; }
  [XmlArrayItem("AuthorName")]
  public string[] Authors { get; set; }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接