2013-09-27 10 views
7

Sto provando a serializzare una classe, scrivendo in un file XML come più frammenti, cioè, scrivi ogni oggetto della classe come un singolo frammento, senza l'intestazione/radice XML. Di seguito è riportato un codice di esempio:.net XmlSerialize genera "WriteStartDocument non può essere chiamato su scrittori creati con ConformanceLevel.Fragment"

[Serializable] 
public class Test 
{ 
    public int X { get; set; } 
    public String Y { get; set; } 
    public String[] Z { get; set; } 

    public Test() 
    { 
    } 

    public Test(int x, String y, String[] z) 
    { 
     X = x; 
     Y = y; 
     Z = z; 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Test t1 = new Test(1, "t1", new[] { "a", "b" }); 
     Test t2 = new Test(2, "t2", new[] { "c", "d", "e" }); 

     XmlSerializer serializer = new XmlSerializer(typeof(Test)); 
     //using (StreamWriter writer = new StreamWriter(@"f:\test\test.xml")) 
     { 
      XmlWriter xmlWriter = XmlWriter.Create(@"f:\test\test.xml", 
                new XmlWriterSettings() 
                 {ConformanceLevel = ConformanceLevel.Fragment, 
                 OmitXmlDeclaration = true, 
                 Indent = true}); 
      serializer.Serialize(xmlWriter, t1); 
      serializer.Serialize(xmlWriter, t2); 
      xmlWriter.Close(); 
     } 
    } 
} 

Nella prima chiamata a serializzare, ottengo l'eccezione:

WriteStartDocument cannot be called on writers created with ConformanceLevel.Fragment 

Che cosa mi manca qui?

risposta

13

esiste una soluzione a questo problema. Quando il writer xml è stato utilizzato prima di utilizzare il serializzatore, l'intestazione non verrà scritta. Il seguente funziona, ma aggiungerà un commento tag vuoto sulla prima riga del file xml

migliorata codice come suggerito da Oleksa

static void Main(string[] args) 
    { 
     Test t1 = new Test(1, "t1", new[] { "a", "b" }); 
     Test t2 = new Test(2, "t2", new[] { "c", "d", "e" }); 

     XmlSerializer serializer = new XmlSerializer(typeof(Test)); 
     //using (StreamWriter writer = new StreamWriter(@"f:\test\test.xml")) 
     { 
      XmlWriter xmlWriter = XmlWriter.Create(@"test.xml", 
                new XmlWriterSettings() 
                { 
                 ConformanceLevel = ConformanceLevel.Fragment, 
                 OmitXmlDeclaration = false, 
                 Indent = true, 
                 NamespaceHandling = NamespaceHandling.OmitDuplicates 
                }); 
      xmlWriter.WriteWhitespace(""); 
      serializer.Serialize(xmlWriter, t1); 
      serializer.Serialize(xmlWriter, t2); 
      xmlWriter.Close(); 
     } 
    } 
+0

Grande, che appena funziona benissimo. Non mi interessa un commento vuoto nel file – sppc42

+0

Nota che la proprietà ['NamespaceHandling'] (http://msdn.microsoft.com/en-us/library/system.xmlwritersettings.namespacehandling.aspx) esiste solo in .NET 4.0 e versioni successive. –

+3

Non è possibile scrivere nulla con 'xmlW.WriteWhitespace (" ")' – oleksa