XNode.WriteTo(XmlWriter) Méthode

Définition

Écrit ce nœud dans un 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)

Paramètres

writer
XmlWriter

Dans XmlWriter laquelle cette méthode écrit.

Exemples

L’exemple suivant crée une XmlWriter écriture dans un StringBuilder. Il utilise ensuite cette méthode pour écrire deux arborescences XML dans l’enregistreur.

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

Cet exemple produit la sortie suivante :

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

Remarques

Vous pouvez utiliser cette méthode pour écrire du code qui effectue une transformation de streaming d’un document très volumineux. Pour plus d’informations, consultez Comment effectuer une transformation de diffusion en continu de documents XML volumineux.

S’applique à

Voir aussi