XElement.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)
参数
示例
下面的示例演示如何将一个 XElement 写入到一个 XmlWriter。 请注意,该示例未编写 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>