次の方法で共有


XElement.WriteTo(XmlWriter) メソッド

定義

この要素を XmlWriter に書き込みます。

public:
 override void WriteTo(System::Xml::XmlWriter ^ writer);
public override void WriteTo (System.Xml.XmlWriter writer);
override this.WriteTo : System.Xml.XmlWriter -> unit
Public Overrides Sub WriteTo (writer As XmlWriter)

パラメーター

writer
XmlWriter

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

次の例は、XElementXmlWriter. この例では 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>  

適用対象

こちらもご覧ください