我需要像这样序列化一个对象:
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>
AuthorName只是一个字符串。如何在不创建字符串包装的情况下执行此操作?
谢谢
答案 0 :(得分:2)
使用XmlArrayItem
属性:
public class Book
{
public string Title { get; set; }
[XmlArrayItem("AuthorName")]
public string[] Authors { get; set; }
}
答案 1 :(得分:2)
public class Book
{
public string Title { get; set; }
[XmlArrayItem("AuthorName")]
public string[] Authors { get; set; }
}