XElement.Save Método
Definição
Serializa a árvore XML subjacente do elemento.Serialize this element's underlying XML tree. A saída pode ser salva em um arquivo, um XmlTextWriter, um TextWriter ou um XmlWriter.The output can be saved to a file, an XmlTextWriter, a TextWriter, or an XmlWriter. Opcionalmente, a formatação (recuo) pode ser desabilitada.Optionally, formatting (indenting) can be disabled.
Sobrecargas
| Save(Stream) |
Gera este XElement para o Stream especificado.Outputs this XElement to the specified Stream. |
| Save(TextWriter) |
Serialize esse elemento para um TextWriter.Serialize this element to a TextWriter. |
| Save(String) |
Serialize esse elemento em um arquivo.Serialize this element to a file. |
| Save(XmlWriter) |
Serialize esse elemento em um XmlWriter.Serialize this element to an XmlWriter. |
| Save(Stream, SaveOptions) |
Gera esse XElement para o Stream especificado, com a opção de especificar o comportamento de formatação.Outputs this XElement to the specified Stream, optionally specifying formatting behavior. |
| Save(TextWriter, SaveOptions) |
Serializa esse elemento para um TextWriter, opcionalmente desabilitando a formatação.Serialize this element to a TextWriter, optionally disabling formatting. |
| Save(String, SaveOptions) |
Serialize esse elemento em um arquivo, opcionalmente, desabilitando a formatação.Serialize this element to a file, optionally disabling formatting. |
Save(Stream)
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)
Parâmetros
Comentários
O XML serializado será recuado.The serialized XML will be indented. Todos os espaços em branco insignificantes serão removidos e espaços em branco adicionais serão adicionados para que o XML seja recuado corretamente.All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. O comportamento desse método é que o espaço em branco insignificante não será preservado.The behavior of this method is that insignificant white space will not be preserved.
Se você quiser controlar o espaço em branco, use a sobrecarga de Save que o usa SaveOptions como um parâmetro.If you want to control white space, use the overload of Save that takes SaveOptions as a parameter. Use a DisableFormatting opção para salvar XML sem recuo.Use the DisableFormatting option to save unindented XML. Isso fará com que o gravador grave todos os espaços em branco exatamente como representado na árvore XML.This will cause the writer to write all white spaces exactly as represented in the XML tree.
Use a OmitDuplicateNamespaces opção se você quiser remover declarações de namespace duplicadas.Use the OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.
Aplica-se a
Save(TextWriter)
Serialize esse elemento para um TextWriter.Serialize this element to a 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)
Parâmetros
- textWriter
- TextWriter
Um TextWriter no qual o XElement será gravado.A TextWriter that the XElement will be written to.
Exemplos
O exemplo a seguir cria um XElement , salva o documento em um StringWriter e, em seguida, imprime a cadeia de caracteres no console.The following example creates an XElement, saves the document to a StringWriter, and then prints the string to the console.
XElement root = XElement.Parse(@"<Root> <Child> Text </Child> </Root>");
using (StringWriter sw = new StringWriter()) {
root.Save(sw);
Console.WriteLine(sw.ToString());
}
Dim root As XElement = <Root><Child> Text </Child></Root>
Using sw = New StringWriter()
root.Save(sw)
Console.WriteLine(sw.ToString())
End Using
Esse exemplo gera a saída a seguir:This example produces the following output:
<?xml version="1.0" encoding="utf-16"?>
<Root>
<Child> Text </Child>
</Root>
Comentários
O XML serializado será recuado.The serialized XML will be indented. Todos os espaços em branco insignificantes serão removidos e espaços em branco adicionais serão adicionados para que o XML seja recuado corretamente.All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. O comportamento desse método é que o espaço em branco não será preservado.The behavior of this method is that white space will not be preserved.
Se você quiser controlar o espaço em branco, use a sobrecarga de Save que permite especificar SaveOptions como um parâmetro.If you want to control white space, use the overload of Save that allows you to specify SaveOptions as a parameter. Para obter mais informações, consulte Preservar espaço em branco ao carregar ou analisar XML e preservar o espaço em branco durante a serialização.For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.
Aplica-se a
Save(String)
Serialize esse elemento em um arquivo.Serialize this element to a file.
public:
void Save(System::String ^ fileName);
public void Save (string fileName);
member this.Save : string -> unit
Public Sub Save (fileName As String)
Parâmetros
Exemplos
O exemplo a seguir cria um XElement , salva o documento em um arquivo e, em seguida, imprime o arquivo no console.The following example creates an XElement, saves the document to a file, and then prints the file to the console.
XElement root = new XElement("Root",
new XElement("Child", "child content")
);
root.Save("Root.xml");
string str = File.ReadAllText("Root.xml");
Console.WriteLine(str);
Dim root As XElement = _
<Root>
<Child>child content</Child>
</Root>
root.Save("Root.xml")
Dim Str As String = File.ReadAllText("Root.xml")
Console.WriteLine(Str)
Esse exemplo gera a saída a seguir:This example produces the following output:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child>child content</Child>
</Root>
Comentários
O XML serializado será recuado.The serialized XML will be indented. Todos os espaços em branco insignificantes serão removidos e espaços em branco adicionais serão adicionados para que o XML seja recuado corretamente.All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. O comportamento desse método é que os nós de espaço em branco insignificantes na árvore XML não serão preservados.The behavior of this method is that insignificant white space nodes in the XML tree will not be preserved.
Se você quiser controlar o espaço em branco, use a sobrecarga de Save que permite especificar SaveOptions como um parâmetro.If you want to control white space, use the overload of Save that allows you to specify SaveOptions as a parameter. Para obter mais informações, consulte Preservar espaço em branco ao carregar ou analisar XML e preservar o espaço em branco durante a serialização.For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.
Aplica-se a
Save(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)
Parâmetros
- writer
- XmlWriter
Um XmlWriter no qual o XElement será gravado.A XmlWriter that the XElement will be written to.
Exemplos
O exemplo a seguir mostra como salvar um XElement em um XmlWriter.The following example shows how to save an XElement to an XmlWriter.
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws)) {
XElement root = new XElement("Root",
new XElement("Child", "child content")
);
root.Save(xw);
}
Console.WriteLine(sb.ToString());
Dim sb As StringBuilder = New StringBuilder()
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.OmitXmlDeclaration = True
Using xw = XmlWriter.Create(sb, xws)
Dim root As XElement = <Root>
<Child>child content</Child>
</Root>
root.Save(xw)
End Using
Console.WriteLine(sb.ToString())
Esse exemplo gera a saída a seguir:This example produces the following output:
<Root><Child>child content</Child></Root>
Aplica-se a
Save(Stream, SaveOptions)
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)
Parâmetros
- options
- SaveOptions
Um objeto SaveOptions que especifica o comportamento de formatação.A SaveOptions object that specifies formatting behavior.
Comentários
Por padrão, o options é definido como None .By default the options are set to None. Esta opção removerá todos os espaços em branco insignificantes relevantes e adicionará um espaço em branco insignificante apropriado para que o XML seja recuado corretamente.This option will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented.
Se você quiser salvar XML sem recuo, especifique o DisableFormatting sinalizador para options .If you want to save unindented XML, specify the DisableFormatting flag for options. Isso fará com que o gravador grave todos os espaços em branco exatamente como representado na árvore XML.This will cause the writer to write all white spaces exactly as represented in the XML tree.
Use a OmitDuplicateNamespaces opção se você quiser remover declarações de namespace duplicadas.Use OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.
Aplica-se a
Save(TextWriter, SaveOptions)
Serializa esse elemento para um TextWriter, opcionalmente desabilitando a formatação.Serialize this element to a TextWriter, optionally disabling formatting.
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)
Parâmetros
- textWriter
- TextWriter
O TextWriter para a saída do XML.The TextWriter to output the XML to.
- options
- SaveOptions
Um SaveOptions que especifica o comportamento de formatação.A SaveOptions that specifies formatting behavior.
Exemplos
O exemplo a seguir mostra dois usos desse método.The following example shows two uses of this method. O primeiro uso preserva o espaço em branco.The first use preserves white space. A segunda serializa a XElement formatação with.The second serializes the XElement with formatting. Como o documento não tem nenhum espaço em branco nele como construído, preservar espaço em branco gera o XML sem nenhum recuo.Because the document has no white space in it as constructed, preserving white space outputs the XML without any indenting.
XElement root = XElement.Parse(@"<Root> <Child> Text </Child> </Root>");
using (StringWriter sw = new StringWriter())
{
root.Save(sw, SaveOptions.DisableFormatting);
Console.WriteLine(sw.ToString());
}
Console.WriteLine("=====");
using (StringWriter sw = new StringWriter())
{
root.Save(sw, SaveOptions.None);
Console.WriteLine(sw.ToString());
}
Dim root As XElement = <Root><Child> Text </Child></Root>
Using sw = New StringWriter()
root.Save(sw, SaveOptions.DisableFormatting)
Console.WriteLine(sw.ToString())
End Using
Console.WriteLine("=====")
Using sw = New StringWriter()
root.Save(sw, SaveOptions.None)
Console.WriteLine(sw.ToString())
End Using
Esse exemplo gera a saída a seguir:This example produces the following output:
<?xml version="1.0" encoding="utf-16"?><Root><Child> Text </Child></Root>
=====
<?xml version="1.0" encoding="utf-16"?>
<Root>
<Child> Text </Child>
</Root>
Comentários
Se você quiser salvar XML sem recuo, especifique o DisableFormatting sinalizador para options .If you want to save unindented XML, specify the DisableFormatting flag for options. Isso fará com que o gravador grave todo o espaço em branco exatamente como representado na árvore XML.This will cause the writer to write all white space exactly as represented in the XML tree.
Se você quiser salvar XML recuado, não especifique o DisableFormatting sinalizador para options .If you want to save indented XML, do not specify the DisableFormatting flag for options. Isso removerá todos os espaços em branco insignificantes relevantes e adicionará espaço em branco insignificante apropriado para que o XML seja recuado corretamente.This will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented. Esse é o comportamento padrão e o comportamento das sobrecargas dos Save métodos que não são necessários options como parâmetro.This is the default behavior, and the behavior of the overloads of the Save methods that do not take options as a parameter.
Para obter mais informações, consulte Preservar espaço em branco ao carregar ou analisar XML e preservar o espaço em branco durante a serialização.For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.
Aplica-se a
Save(String, SaveOptions)
Serialize esse elemento em um arquivo, opcionalmente, desabilitando a formatação.Serialize this element to a file, optionally disabling formatting.
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)
Parâmetros
- options
- SaveOptions
Um SaveOptions que especifica o comportamento de formatação.A SaveOptions that specifies formatting behavior.
Exemplos
O exemplo a seguir mostra dois usos desse método.The following example shows two uses of this method. O primeiro uso preserva o espaço em branco.The first use preserves white space. O segundo serializa a XElement formatação with.The second one serializes the XElement with formatting.
string str;
XElement root = XElement.Parse(@"<Root> <Child> Text </Child> </Root>");
root.Save("Root.xml", SaveOptions.DisableFormatting);
str = File.ReadAllText("Root.xml");
Console.WriteLine(str);
Console.WriteLine("=====");
root.Save("Root.xml", SaveOptions.None);
str = File.ReadAllText("Root.xml");
Console.WriteLine(str);
Dim str As String
Dim root As XElement = <Root><Child> Text </Child></Root>
root.Save("Root.xml", SaveOptions.DisableFormatting)
str = File.ReadAllText("Root.xml")
Console.WriteLine(str)
Console.WriteLine("=====")
root.Save("Root.xml", SaveOptions.None)
str = File.ReadAllText("Root.xml")
Console.WriteLine(str)
Esse exemplo gera a saída a seguir:This example produces the following output:
<?xml version="1.0" encoding="utf-8"?><Root><Child> Text </Child></Root>
=====
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child> Text </Child>
</Root>
Comentários
Se você quiser salvar XML sem recuo, especifique o DisableFormatting sinalizador para options .If you want to save unindented XML, specify the DisableFormatting flag for options. Isso fará com que o gravador grave todo o espaço em branco exatamente como representado na árvore XML.This will cause the writer to write all white space exactly as represented in the XML tree.
Se você quiser salvar XML recuado, não especifique o DisableFormatting sinalizador para options .If you want to save indented XML, do not specify the DisableFormatting flag for options. Isso removerá todos os espaços em branco insignificantes relevantes e adicionará espaço em branco insignificante apropriado para que o XML seja recuado corretamente.This will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented. Esse é o comportamento padrão e o comportamento das sobrecargas dos Save métodos que não são necessários options como parâmetro.This is the default behavior, and the behavior of the overloads of the Save methods that do not take options as a parameter.
Para obter mais informações, consulte Preservar espaço em branco ao carregar ou analisar XML e preservar o espaço em branco durante a serialização.For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.