views:

28

answers:

2

Hi,

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"&gt;
  <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"&gt;
  <Title>Good Book</Title>
  <Authors>
        <AuthorName>Author1</AuthorName>
        <AuthorName>Author2</AuthorName>
  </Authors>
</Book>

The AuthorName is just a string. How can I do this without creating string wrapper?

Thank you

+2  A: 

Use the XmlArrayItem attribute:

public class Book
{
    public string Title { get; set; }

    [XmlArrayItem("AuthorName")]
    public string[] Authors { get; set; }
}
Justin Niessner
Thanks! That worked.
tvr
+2  A: 

Use the XmlArrayItem attribute:

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