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。 接著會使用此方法為寫入者寫入兩條 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 文件的串流轉換

適用於

另請參閱