Język

XStreamingElement.Save Metoda

Definicja

Serializuj ten element przesyłania strumieniowego. Dane wyjściowe można zapisać w pliku , XmlTextWriter, lub TextWriterXmlWriter. Opcjonalnie można wyłączyć formatowanie (wcięcia).

Przeciążenia

Nazwa Opis
Save(Stream)

Zwraca to XStreamingElement do określonego Streamelementu .

Save(TextWriter)

Serializuj ten element przesyłania strumieniowego do elementu TextWriter.

Save(String)

Serializuj ten element przesyłania strumieniowego do pliku.

Save(XmlWriter)

Serializuj ten element przesyłania strumieniowego do elementu XmlWriter.

Save(Stream, SaveOptions)

Zwraca to XStreamingElement do określonego Streamelementu , opcjonalnie określając zachowanie formatowania.

Save(TextWriter, SaveOptions)

Serializuj ten element przesyłania strumieniowego TextWriterdo elementu , opcjonalnie wyłączając formatowanie.

Save(String, SaveOptions)

Serializuj ten element przesyłania strumieniowego do pliku, opcjonalnie wyłączając formatowanie.

Save(Stream)

Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs

Zwraca to XStreamingElement do określonego Streamelementu .

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)

Parametry

stream
Stream

Strumień do danych wyjściowych XDocument .

Uwagi

Serializowany kod XML zostanie wcięty. Wszystkie nieistotne białe znaki zostaną usunięte, a dodatkowe białe znaki zostaną dodane tak, aby kod XML został prawidłowo wcięty. Zachowanie tej metody polega na tym, że nie będzie zachowywane nieistotne białe znaki.

Jeśli chcesz kontrolować biały znak, użyj przeciążenia Save , które przyjmuje SaveOptions jako parametr. DisableFormatting Użyj opcji , aby zapisać niezidentyfikowany kod XML. Spowoduje to zapisanie wszystkich białych spacji dokładnie tak, jak pokazano w drzewie XML.

Użyj OmitDuplicateNamespaces opcji , jeśli chcesz usunąć zduplikowane deklaracje przestrzeni nazw.

Dotyczy

Save(TextWriter)

Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs

Serializuj ten element przesyłania strumieniowego do elementu 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)

Parametry

textWriter
TextWriter

Element TextWriter , do którego XStreamingElement zostanie zapisany.

Przykłady

Poniższy przykład tworzy źródłowe drzewo XML, a następnie tworzy wystąpienie XStreamingElement przy użyciu zapytania w drzewie źródłowym XML. Następnie zapisuje element przesyłania strumieniowego do elementu StringWriter.

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())

Ten przykład generuje następujące wyniki:

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

Uwagi

Serializowany kod XML zostanie wcięty. Wszystkie nieistotne białe znaki zostaną usunięte, a dodatkowe białe znaki zostaną dodane tak, aby kod XML został prawidłowo wcięty. Zachowanie tej metody polega na tym, że nieistotne węzły odstępu w drzewie XML nie zostaną zachowane.

Jeśli chcesz kontrolować biały znak, użyj jednego z przeciążeń Save , które przyjmują SaveOptions jako parametr. Aby uzyskać więcej informacji, zobacz Zachowywanie odstępu podczas ładowania lub analizowania kodu XML i Zachowywanie odstępu podczas serializacji.

Zobacz też

Dotyczy

Save(String)

Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs

Serializuj ten element przesyłania strumieniowego do pliku.

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

Parametry

fileName
String

Element String zawierający nazwę pliku.

Przykłady

Poniższy przykład tworzy drzewo XML przesyłania strumieniowego. Następnie serializuje drzewo XML przesyłania strumieniowego do pliku.

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"))

Ten przykład generuje następujące wyniki:

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

Uwagi

Serializowany kod XML zostanie wcięty. Wszystkie nieistotne białe znaki zostaną usunięte, a dodatkowe białe znaki zostaną dodane tak, aby kod XML został prawidłowo wcięty. Zachowanie tej metody polega na tym, że nieistotne węzły odstępu w drzewie XML nie zostaną zachowane.

Jeśli chcesz kontrolować biały znak, użyj jednego z przeciążeń Save , które przyjmują SaveOptions jako parametr. Aby uzyskać więcej informacji, zobacz Zachowywanie odstępu podczas ładowania lub analizowania kodu XML i Zachowywanie odstępu podczas serializacji.

Zobacz też

Dotyczy

Save(XmlWriter)

Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs

Serializuj ten element przesyłania strumieniowego do elementu 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)

Parametry

writer
XmlWriter

Element XmlWriter , do którego XElement zostanie zapisany.

Przykłady

Poniższy przykład tworzy obiekt XStreamingElement i zapisuje go w obiekcie XmlWriter.

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())

Ten przykład generuje następujące wyniki:

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

Zobacz też

Dotyczy

Save(Stream, SaveOptions)

Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs

Zwraca to XStreamingElement do określonego Streamelementu , opcjonalnie określając zachowanie formatowania.

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)

Parametry

stream
Stream

Strumień do danych wyjściowych XDocument .

options
SaveOptions

SaveOptions Obiekt określający zachowanie formatowania.

Uwagi

Domyślnie parametr ma options wartość None. Ta opcja spowoduje usunięcie wszystkich nadmiarowych nieistotnych białych znaków i dodanie odpowiedniego nieistotnego odstępu, tak aby kod XML był poprawnie wcięty.

Jeśli chcesz zapisać niezidentyfikowany kod XML, określ flagę DisableFormatting .options Spowoduje to zapisanie wszystkich białych spacji dokładnie tak, jak pokazano w drzewie XML.

Użyj OmitDuplicateNamespaces opcji , jeśli chcesz usunąć zduplikowane deklaracje przestrzeni nazw.

Dotyczy

Save(TextWriter, SaveOptions)

Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs

Serializuj ten element przesyłania strumieniowego TextWriterdo elementu , opcjonalnie wyłączając formatowanie.

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)

Parametry

textWriter
TextWriter

W TextWriter celu wyprowadzenia pliku XML do.

options
SaveOptions

Element SaveOptions określający zachowanie formatowania.

Przykłady

W poniższym przykładzie przedstawiono dwa zastosowania tej metody. Pierwsze użycie zachowuje biały znak. Drugi serializuje element XStreamingElement z formatowaniem.

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())

Ten przykład generuje następujące wyniki:

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

Uwagi

Jeśli chcesz zapisać niezidentyfikowany kod XML, określ flagę DisableFormatting .options Spowoduje to zapisanie wszystkich białych znaków dokładnie tak, jak pokazano w drzewie XML.

Jeśli chcesz zapisać wcięty kod XML, nie należy określać flagi DisableFormatting dla optionselementu . Spowoduje to usunięcie wszystkich nadmiarowych nieistotnych białych znaków i dodanie odpowiedniego nieistotnego odstępu, tak aby kod XML był prawidłowo wcięty. Jest to zachowanie domyślne i zachowanie przeciążeń Save metod, które nie przyjmują options jako parametru.

Aby uzyskać więcej informacji, zobacz Zachowywanie odstępu podczas ładowania lub analizowania kodu XML i Zachowywanie odstępu podczas serializacji.

Zobacz też

Dotyczy

Save(String, SaveOptions)

Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs
Źródło:
XStreamingElement.cs

Serializuj ten element przesyłania strumieniowego do pliku, opcjonalnie wyłączając formatowanie.

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)

Parametry

fileName
String

Element String zawierający nazwę pliku.

options
SaveOptions

SaveOptions Obiekt określający zachowanie formatowania.

Przykłady

W poniższym przykładzie przedstawiono dwa zastosowania tej metody. Pierwsze użycie zachowuje biały znak. Drugi serializuje element XStreamingElement z formatowaniem.

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"))

Ten przykład generuje następujące wyniki:

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

Uwagi

Jeśli chcesz zapisać niezidentyfikowany kod XML, określ flagę DisableFormatting .options Spowoduje to zapisanie wszystkich białych znaków dokładnie tak, jak pokazano w drzewie XML.

Jeśli chcesz zapisać wcięty kod XML, nie należy określać flagi DisableFormatting dla optionselementu . Spowoduje to usunięcie wszystkich nadmiarowych nieistotnych białych znaków i dodanie odpowiedniego nieistotnego odstępu, tak aby kod XML był prawidłowo wcięty. Jest to zachowanie domyślne i zachowanie przeciążeń Save metod, które nie przyjmują options jako parametru.

Aby uzyskać więcej informacji, zobacz Zachowywanie odstępu podczas ładowania lub analizowania kodu XML i Zachowywanie odstępu podczas serializacji.

Zobacz też

Dotyczy