Share via


XStreamingElement.Save Metodo

Definizione

Serializzare questo elemento di flusso. L'output può essere salvato in un file oppure in un oggetto XmlTextWriter, TextWriter o XmlWriter. Facoltativamente, è possibile disabilitare la formattazione (rientro).

Overload

Save(Stream)

Restituisce l'oggetto XStreamingElement nell'oggetto Stream specificato.

Save(TextWriter)

Serializzare questo elemento di flusso in un TextWriter.

Save(String)

Serializzare questo elemento di flusso in un file.

Save(XmlWriter)

Serializzare questo elemento di flusso in un XmlWriter.

Save(Stream, SaveOptions)

Restituisce l'oggetto XStreamingElement nell'oggetto Stream specificato, indicando facoltativamente il comportamento di formattazione.

Save(TextWriter, SaveOptions)

Serializzare questo elemento di flusso in un TextWriter, disabilitando facoltativamente la formattazione.

Save(String, SaveOptions)

Serializzare questo elemento di flusso in un file, disabilitando facoltativamente la formattazione.

Save(Stream)

Restituisce l'oggetto XStreamingElement nell'oggetto Stream specificato.

public:
 void Save(System::IO::Stream ^ stream);
public void Save (System.IO.Stream stream);
member this.Save : System.IO.Stream -> unit
Public Sub Save (stream As Stream)

Parametri

stream
Stream

Flusso in cui restituire l'oggetto XDocument.

Commenti

Il codice XML serializzato verrà rientro. Tutti gli spazi vuoti insignificanti verranno rimossi e verrà aggiunto spazio vuoto aggiuntivo in modo che l'XML venga rientro corretto. Il comportamento di questo metodo è che lo spazio vuoto insignificante non verrà mantenuto.

Se si vuole controllare lo spazio vuoto, usare l'overload di Save che accetta SaveOptions come parametro. Usare l'opzione DisableFormatting per salvare xml non rientri. In questo modo il writer scriverà tutti gli spazi vuoti esattamente come rappresentati nell'albero XML.

Usare OmitDuplicateNamespaces l'opzione se si desidera rimuovere dichiarazioni di spazio dei nomi duplicati.

Si applica a

Save(TextWriter)

Serializzare questo elemento di flusso in un TextWriter.

public:
 void Save(System::IO::TextWriter ^ textWriter);
public void Save (System.IO.TextWriter textWriter);
member this.Save : System.IO.TextWriter -> unit
Public Sub Save (textWriter As TextWriter)

Parametri

textWriter
TextWriter

Oggetto TextWriter in cui verrà scritto l'oggetto XStreamingElement.

Esempio

Nell'esempio seguente viene creato un albero XML di origine, quindi viene creata un'istanza di una XStreamingElement query nell'albero XML di origine. Scrive quindi l'elemento di streaming in un StringWriteroggetto .

XElement srcTree = new XElement("Root",  
                       new XElement("Child", 1),  
                       new XElement("Child", 2),  
                       new XElement("Child", 3),  
                       new XElement("Child", 4),  
                       new XElement("Child", 5)  
                   );  

XStreamingElement dstTree = new XStreamingElement("NewRoot",  
                        from el in srcTree.Elements()  
                        where (int)el >= 3  
                        select new XElement("DifferentChild", (int)el)  
                    );  

StringBuilder sb = new StringBuilder();  
dstTree.Save(new StringWriter(sb));  
Console.WriteLine(sb.ToString());  
Dim srcTree As XElement = _  
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  

Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _  
                        From el In srcTree.Elements() _  
                        Where el.Value >= 3 _  
                        Select <DifferentChild><%= el.Value %></DifferentChild> _  
                    )  

Dim sb As StringBuilder = New StringBuilder()  
dstTree.Save(New StringWriter(sb))  
Console.WriteLine(sb.ToString())  

Nell'esempio viene prodotto l'output seguente:

<?xml version="1.0" encoding="utf-16"?>  
<NewRoot>  
  <DifferentChild>3</DifferentChild>  
  <DifferentChild>4</DifferentChild>  
  <DifferentChild>5</DifferentChild>  
</NewRoot>  

Commenti

Il codice XML serializzato verrà rientro. Tutti gli spazi vuoti insignificanti verranno rimossi e verrà aggiunto spazio vuoto aggiuntivo in modo che l'XML venga rientro corretto. Il comportamento di questo metodo è che i nodi di spazio vuoto insignificanti nell'albero XML non verranno mantenuti.

Se si vuole controllare lo spazio vuoto, usare uno degli overload di Save che accettano SaveOptions come parametro. Per altre informazioni, vedere Mantenere lo spazio vuoto durante il caricamento o l'analisi di XML e La conservazione dello spazio vuoto durante la serializzazione.

Vedi anche

Si applica a

Save(String)

Serializzare questo elemento di flusso in un file.

public:
 void Save(System::String ^ fileName);
public void Save (string fileName);
member this.Save : string -> unit
Public Sub Save (fileName As String)

Parametri

fileName
String

Oggetto String che contiene il nome del file.

Esempio

Nell'esempio seguente viene creato un albero XML di streaming. Serializza quindi l'albero XML di streaming in un file.

XElement srcTree = new XElement("Root",  
                       new XElement("Child", 1),  
                       new XElement("Child", 2),  
                       new XElement("Child", 3),  
                       new XElement("Child", 4),  
                       new XElement("Child", 5)  
                   );  

XStreamingElement dstTree = new XStreamingElement("NewRoot",  
                        from el in srcTree.Elements()  
                        where (int)el >= 3  
                        select new XElement("DifferentChild", (int)el)  
                    );  

dstTree.Save("Test.xml");  
Console.WriteLine(File.ReadAllText("Test.xml"));  
Dim srcTree As XElement = _  
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  

Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _  
                        From el In srcTree.Elements() _  
                        Where el.Value >= 3 _  
                        Select <DifferentChild><%= el.Value %></DifferentChild> _  
                    )  

dstTree.Save("Test.xml")  
Console.WriteLine(File.ReadAllText("Test.xml"))  

Nell'esempio viene prodotto l'output seguente:

<?xml version="1.0" encoding="utf-8"?>  
<NewRoot>  
  <DifferentChild>3</DifferentChild>  
  <DifferentChild>4</DifferentChild>  
  <DifferentChild>5</DifferentChild>  
</NewRoot>  

Commenti

Il codice XML serializzato verrà rientro. Tutti gli spazi vuoti insignificanti verranno rimossi e verrà aggiunto spazio vuoto aggiuntivo in modo che l'XML venga rientro corretto. Il comportamento di questo metodo è che i nodi di spazio vuoto insignificanti nell'albero XML non verranno mantenuti.

Se si vuole controllare lo spazio vuoto, usare uno degli overload di Save che accettano SaveOptions come parametro. Per altre informazioni, vedere Mantenere lo spazio vuoto durante il caricamento o l'analisi di XML e La conservazione dello spazio vuoto durante la serializzazione.

Vedi anche

Si applica a

Save(XmlWriter)

Serializzare questo elemento di flusso in un XmlWriter.

public:
 void Save(System::Xml::XmlWriter ^ writer);
public void Save (System.Xml.XmlWriter writer);
member this.Save : System.Xml.XmlWriter -> unit
Public Sub Save (writer As XmlWriter)

Parametri

writer
XmlWriter

Oggetto XmlWriter in cui verrà scritto l'oggetto XElement.

Esempio

L'esempio seguente crea un oggetto XStreamingElement e lo scrive in un XmlWriteroggetto .

XElement srcTree = new XElement("Root",  
                       new XElement("Child", 1),  
                       new XElement("Child", 2),  
                       new XElement("Child", 3),  
                       new XElement("Child", 4),  
                       new XElement("Child", 5)  
                   );  

StringBuilder sb = new StringBuilder();  
XmlWriterSettings xws = new XmlWriterSettings();  
xws.OmitXmlDeclaration = true;  
using (XmlWriter xw = XmlWriter.Create(sb, xws))  
{  
    XStreamingElement dstTree = new XStreamingElement("NewRoot",  
                            from el in srcTree.Elements()  
                            where (int)el == 5  
                            select new XElement("DifferentChild", (int)el)  
                        );  

    dstTree.Save(xw);  
}  
Console.WriteLine(sb.ToString());  
Dim srcTree As XElement = _  
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  

Dim sb As StringBuilder = New StringBuilder()  
Dim xws As XmlWriterSettings = New XmlWriterSettings()  
xws.OmitXmlDeclaration = True  
Using xw As XmlWriter = XmlWriter.Create(sb, xws)  
    Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _  
                            From el In srcTree.Elements() _  
                            Where el.Value = 5 _  
                            Select <DifferentChild><%= el.Value %></DifferentChild> _  
                        )  
    dstTree.Save(xw)  
End Using  
Console.WriteLine(sb.ToString())  

Nell'esempio viene prodotto l'output seguente:

<NewRoot><DifferentChild>5</DifferentChild></NewRoot>  

Vedi anche

Si applica a

Save(Stream, SaveOptions)

Restituisce l'oggetto XStreamingElement nell'oggetto Stream specificato, indicando facoltativamente il comportamento di formattazione.

public:
 void Save(System::IO::Stream ^ stream, System::Xml::Linq::SaveOptions options);
public void Save (System.IO.Stream stream, System.Xml.Linq.SaveOptions options);
member this.Save : System.IO.Stream * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (stream As Stream, options As SaveOptions)

Parametri

stream
Stream

Flusso in cui restituire l'oggetto XDocument.

options
SaveOptions

Oggetto SaveOptions che specifica il comportamento della formattazione.

Commenti

Per impostazione predefinita, l'oggetto options è impostato su None. Questa opzione rimuoverà tutti gli spazi vuoti extranei e aggiungerà spazi vuoti insignificanti appropriati in modo che il file XML venga rientro correttamente.

Se si vuole salvare il codice XML non rientro, specificare il DisableFormatting flag per options. In questo modo il writer scriverà tutti gli spazi vuoti esattamente come rappresentati nell'albero XML.

Usare OmitDuplicateNamespaces l'opzione se si desidera rimuovere dichiarazioni di spazio dei nomi duplicati.

Si applica a

Save(TextWriter, SaveOptions)

Serializzare questo elemento di flusso in un TextWriter, disabilitando facoltativamente la formattazione.

public:
 void Save(System::IO::TextWriter ^ textWriter, System::Xml::Linq::SaveOptions options);
public void Save (System.IO.TextWriter textWriter, System.Xml.Linq.SaveOptions options);
member this.Save : System.IO.TextWriter * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (textWriter As TextWriter, options As SaveOptions)

Parametri

textWriter
TextWriter

Oggetto TextWriter nel quale deve essere restituito il codice XML.

options
SaveOptions

Oggetto SaveOptions che specifica il comportamento di formattazione.

Esempio

Nell'esempio seguente vengono illustrati due usi di questo metodo. Il primo utilizzo mantiene lo spazio vuoto. Il secondo serializza l'oggetto XStreamingElement con la formattazione.

XElement srcTree = new XElement("Root",  
                       new XElement("Child", 1),  
                       new XElement("Child", 2),  
                       new XElement("Child", 3),  
                       new XElement("Child", 4),  
                       new XElement("Child", 5)  
                   );  

XStreamingElement dstTree = new XStreamingElement("NewRoot",  
                        from el in srcTree.Elements()  
                        where (int)el == 3  
                        select new XElement("DifferentChild", (int)el)  
                    );  

StringBuilder sb = new StringBuilder();  
dstTree.Save(new StringWriter(sb), SaveOptions.DisableFormatting);  
Console.WriteLine(sb.ToString());  
Console.WriteLine("------");  
sb = new StringBuilder();  
dstTree.Save(new StringWriter(sb), SaveOptions.None);  
Console.WriteLine(sb.ToString());  
Dim srcTree As XElement = _  
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  

Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _  
                        From el In srcTree.Elements() _  
                        Where el.Value = 3 _  
                        Select <DifferentChild><%= el.Value %></DifferentChild> _  
                    )  

Dim sb As StringBuilder = New StringBuilder()  
dstTree.Save(New StringWriter(sb), SaveOptions.DisableFormatting)  
Console.WriteLine(sb.ToString())  
Console.WriteLine("------")  
sb = New StringBuilder()  
dstTree.Save(New StringWriter(sb), SaveOptions.None)  
Console.WriteLine(sb.ToString())  

Nell'esempio viene prodotto l'output seguente:

<?xml version="1.0" encoding="utf-16"?><NewRoot><DifferentChild>3</DifferentChild></NewRoot>  
------  
<?xml version="1.0" encoding="utf-16"?>  
<NewRoot>  
  <DifferentChild>3</DifferentChild>  
</NewRoot>  

Commenti

Se si vuole salvare il codice XML non rientro, specificare il DisableFormatting flag per options. In questo modo il writer scriverà tutti gli spazi vuoti esattamente come rappresentati nell'albero XML.

Se si vuole salvare xml con rientro, non specificare il DisableFormatting flag per options. Ciò rimuoverà tutti gli spazi vuoti irrilevanti e aggiungerà spazi vuoti significativi appropriati in modo che il file XML venga rientro correttamente. Si tratta del comportamento predefinito e del comportamento degli overload dei Save metodi che non accettano options come parametro.

Per altre informazioni, vedere Mantenere lo spazio vuoto durante il caricamento o l'analisi di XML e La conservazione dello spazio vuoto durante la serializzazione.

Vedi anche

Si applica a

Save(String, SaveOptions)

Serializzare questo elemento di flusso in un file, disabilitando facoltativamente la formattazione.

public:
 void Save(System::String ^ fileName, System::Xml::Linq::SaveOptions options);
public void Save (string fileName, System.Xml.Linq.SaveOptions options);
member this.Save : string * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (fileName As String, options As SaveOptions)

Parametri

fileName
String

Oggetto String che contiene il nome del file.

options
SaveOptions

Oggetto SaveOptions che specifica il comportamento della formattazione.

Esempio

Nell'esempio seguente vengono illustrati due usi di questo metodo. Il primo utilizzo mantiene lo spazio vuoto. Il secondo serializza l'oggetto XStreamingElement con la formattazione.

XElement srcTree = new XElement("Root",  
                       new XElement("Child", 1),  
                       new XElement("Child", 2),  
                       new XElement("Child", 3),  
                       new XElement("Child", 4),  
                       new XElement("Child", 5)  
                   );  

XStreamingElement dstTree = new XStreamingElement("NewRoot",  
                        from el in srcTree.Elements()  
                        where (int)el == 3  
                        select new XElement("DifferentChild", (int)el)  
                    );  

dstTree.Save("Test1.xml", SaveOptions.DisableFormatting);  
dstTree.Save("Test2.xml", SaveOptions.None);  
Console.WriteLine(File.ReadAllText("Test1.xml"));  
Console.WriteLine("------");  
Console.WriteLine(File.ReadAllText("Test2.xml"));  
Dim srcTree As XElement = _  
    <Root>  
        <Child>1</Child>  
        <Child>2</Child>  
        <Child>3</Child>  
        <Child>4</Child>  
        <Child>5</Child>  
    </Root>  

Dim dstTree As XStreamingElement = New XStreamingElement("NewRoot", _  
                        From el In srcTree.Elements() _  
                        Where el.Value = 3 _  
                        Select <DifferentChild><%= el.Value %></DifferentChild> _  
                    )  

dstTree.Save("Test1.xml", SaveOptions.DisableFormatting)  
dstTree.Save("Test2.xml", SaveOptions.None)  
Console.WriteLine(File.ReadAllText("Test1.xml"))  
Console.WriteLine("------")  
Console.WriteLine(File.ReadAllText("Test2.xml"))  

Nell'esempio viene prodotto l'output seguente:

<?xml version="1.0" encoding="utf-8"?><NewRoot><DifferentChild>3</DifferentChild></NewRoot>  
------  
<?xml version="1.0" encoding="utf-8"?>  
<NewRoot>  
  <DifferentChild>3</DifferentChild>  
</NewRoot>  

Commenti

Se si vuole salvare il codice XML non rientro, specificare il DisableFormatting flag per options. In questo modo il writer scriverà tutti gli spazi vuoti esattamente come rappresentati nell'albero XML.

Se si vuole salvare xml con rientro, non specificare il DisableFormatting flag per options. Ciò rimuoverà tutti gli spazi vuoti irrilevanti e aggiungerà spazi vuoti significativi appropriati in modo che il file XML venga rientro correttamente. Si tratta del comportamento predefinito e del comportamento degli overload dei Save metodi che non accettano options come parametro.

Per altre informazioni, vedere Mantenere lo spazio vuoto durante il caricamento o l'analisi di XML e La conservazione dello spazio vuoto durante la serializzazione.

Vedi anche

Si applica a