XDocument.WriteTo(XmlWriter) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此文档写入 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)
参数
示例
下面的示例演示如何将一个 XDocument 写入到一个 XmlWriter。 请注意,该示例未编写 XML 声明。
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws))
{
XDocument doc = new XDocument(
new XElement("Child",
new XElement("GrandChild", "some content")
)
);
doc.WriteTo(xw);
}
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)
Dim doc As XDocument = New XDocument(<Child><GrandChild>some content</GrandChild></Child>)
doc.WriteTo(xw)
End Using
Console.WriteLine(sb.ToString())
该示例产生下面的输出:
<Child>
<GrandChild>some content</GrandChild>
</Child>