C# .NET XML序列化数组/列表对象别名
我需要像这样序列化一个对象:C# .NET XML序列化数组/列表对象别名,c#,.net,xml-serialization,C#,.net,Xml Serialization,我需要像这样序列化一个对象: 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
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>
这会产生如下结果:
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>
好书
作者1
作者2
我正在寻找类似于:
<?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>
好书
作者1
作者2
AuthorName只是一个字符串。如何在不创建字符串包装器的情况下完成此操作
谢谢使用属性:
public class Book
{
public string Title { get; set; }
[XmlArrayItem("AuthorName")]
public string[] Authors { get; set; }
}
使用: