次の方法で共有


XNode.WriteTo(XmlWriter) メソッド

定義

このノードを 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)

パラメーター

writer
XmlWriter

このメソッドの書き込み対象の XmlWriter

次の例では、 XmlWriter に書き込む を StringBuilder作成します。 次に、このメソッドを使用して、2 つの XML ツリーをライターに書き込みます。

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

この例を実行すると、次の出力が生成されます。

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

注釈

このメソッドを使用すると、非常に大きなドキュメントのストリーミング変換を行うコードを記述できます。 詳細については、「 大きな XML ドキュメントのストリーミング変換を実行する方法」を参照してください。

適用対象

こちらもご覧ください