XML Array Attribute / .NET XML Serialization

I need to serialize an object like this:

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

This generates something like this:

 <?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> 

I am looking for something like:

 <?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 is just a string. How can I do this without creating a string wrapper?

thanks

+4
source share
2 answers

Use the XmlArrayItem attribute:

 public class Book { public string Title { get; set; } [XmlArrayItem("AuthorName")] public string[] Authors { get; set; } } 
+2
source

Use the XmlArrayItem attribute:

 public class Book { public string Title { get; set; } [XmlArrayItem("AuthorName")] public string[] Authors { get; set; } } 
+2
source

All Articles