XNode.WriteTo(XmlWriter) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此节点写入 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)
参数
示例
以下示例创建一个 XmlWriter 写入到 a 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 文档执行流式转换。