2010-08-31 5 views
4

je dois sérialiser un objet comme celui-ci:.NET sérialisation XML Array/Liste alias d'objet

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

Cela génère quelque chose comme ceci:

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

Je cherche quelque chose comme:

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

Le nom d'auteur est juste une chaîne. Comment puis-je faire cela sans créer de wrapper de chaîne?

Merci

Répondre

2

Utilisez l'attribut XmlArrayItem:

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

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

Merci! Ça a marché. – tvr

2

Utilisez le :

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