2010-11-18 4 views

Répondre

7

XmlSerializer n'est pas responsable de cette - XmlWriter est donc la clé est ici pour créer un objet XmlWriterSettings avec .OmitXmlDeclaration ensemble à vrai et passer que lors de la construction du XmlWriter:

using System.Xml; 
using System.Xml.Serialization; 
public class Foo 
{ 
    public string Bar { get; set; } 
} 
static class Program 
{ 
    static void Main() 
    { 
     XmlWriterSettings settings = new XmlWriterSettings(); 
     settings.OmitXmlDeclaration = true; 
     using (XmlWriter writer = XmlWriter.Create("my.xml", settings)) 
     { 
      XmlSerializer ser = new XmlSerializer(typeof(Foo)); 
      Foo foo = new Foo(); 
      foo.Bar = "abc"; 
      ser.Serialize(writer, foo); 
     } 

    } 
} 
+0

+1 Un peu plus vite :) –

+0

pouvons-nous utiliser TxtWriter pour obtenir ceci? – user496949

+0

@ user496949 sure; utilisez simplement une surcharge différente de 'XmlWriter.Create' et passez le' TextWriter' et le 'XmlWriterSettings'. –

Questions connexes