Prévenir XmlSerializer de la mise en forme de la sortie

Lorsque vous utilisez les paramètres par défaut avec le XmlSerializer il sera de sortie le format XML comme un formatage de valeur.

C'est à dire: quelque chose le long de ces lignes.

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfStock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Stock>
    <ProductCode>12345</ProductCode>
    <ProductPrice>10.32</ProductPrice>
  </Stock>
  <Stock>
    <ProductCode>45632</ProductCode>
    <ProductPrice>5.43</ProductPrice>
  </Stock>
</ArrayOfStock>

Comment éviter tout type de mise en forme de la sortie? Donc ce que je cherche à réaliser est ce.

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfStock xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Stock>
        <ProductCode>123456</ProductCode>
        <ProductPrice>10.57</ProductPrice>
    </Stock>
    <Stock>
        <ProductCode>789123</ProductCode>
        <ProductPrice>133.22</ProductPrice>
    </Stock>
</ArrayOfStock>

EDIT: Le code complet de ma méthode est

public static String Serialize(Stock stock)
{
     XmlSerializer serializer = new XmlSerializer(typeof(Stock));

     using (StringWriter stringWriter = new StringWriter())
     {
         serializer.Serialize(stringWriter, stock);
         return stringWriter.ToString();
     }            
}
Pourquoi la mise en forme de la matière? Est-il à l'origine du problème?

OriginalL'auteur Maxim Gershkovich | 2011-03-24