XNode.WriteTo(XmlWriter) Método

Definição

Escreve este nó num XmlWriter.

public:
 abstract void WriteTo(System::Xml::XmlWriter ^ writer);
public abstract void WriteTo(System.Xml.XmlWriter writer);
abstract member WriteTo : System.Xml.XmlWriter -> unit
Public MustOverride Sub WriteTo (writer As XmlWriter)

Parâmetros

writer
XmlWriter

Um XmlWriter em que este método irá escrever.

Exemplos

O exemplo seguinte cria um XmlWriter que escreve para um StringBuilder. Depois, utiliza este método para escrever duas árvores XML para o autor.

StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;

using (XmlWriter xw = XmlWriter.Create(sb, xws)) {
    xw.WriteStartElement("Root");
    XElement child1 = new XElement("Child",
        new XElement("GrandChild", "some content")
    );
    child1.WriteTo(xw);
    XElement child2 = new XElement("AnotherChild",
        new XElement("GrandChild", "different content")
    );
    child2.WriteTo(xw);
    xw.WriteEndElement();
}
Console.WriteLine(sb.ToString());
Dim sb As StringBuilder = New StringBuilder()
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.OmitXmlDeclaration = True
xws.Indent = True

Using xw = XmlWriter.Create(sb, xws)
    xw.WriteStartElement("Root")
    Dim child1 As XElement = <Child>
                                 <GrandChild>some content</GrandChild>
                             </Child>
    child1.WriteTo(xw)
    Dim child2 As XElement = <AnotherChild>
                                 <GrandChild>different content</GrandChild>
                             </AnotherChild>
    child2.WriteTo(xw)
    xw.WriteEndElement()
End Using

Console.WriteLine(sb.ToString())

Este exemplo produz a seguinte saída:

<Root>
  <Child>
    <GrandChild>some content</GrandChild>
  </Child>
  <AnotherChild>
    <GrandChild>different content</GrandChild>
  </AnotherChild>
</Root>

Observações

Podes usar este método para escrever código que faz uma transformação em streaming de um documento muito grande. Para mais informações, veja Como realizar transformação em streaming de documentos XML grandes.

Aplica-se a

Ver também