XStreamingElement.WriteTo(XmlWriter) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此流元素写入 XmlWriter。
public:
void WriteTo(System::Xml::XmlWriter ^ writer);
public void WriteTo (System.Xml.XmlWriter writer);
member this.WriteTo : System.Xml.XmlWriter -> unit
Public Sub WriteTo (writer As XmlWriter)
参数
示例
The following example creates an XML tree using XStreamingElement. 然后,它将流式处理元素写入到 .XmlWriter
XElement srcTree = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
new XElement("Child", 3),
new XElement("Child", 4),
new XElement("Child", 5)
);
XStreamingElement dstTree = new XStreamingElement("NewRoot",
from el in srcTree.Elements()
where (int)el == 3
select new XElement("DifferentChild", (int)el)
);
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws))
{
dstTree.WriteTo(xw);
}
Console.WriteLine(sb.ToString());
Dim srcTree As XElement = _
<Root>
<Child>1</Child>
<Child>2</Child>
<Child>3</Child>
<Child>4</Child>
<Child>5</Child>
</Root>
Dim dstTree As XStreamingElement = _
New XStreamingElement("NewRoot", _
From el In srcTree.Elements() _
Where el.Value = 3 _
Select <DifferentChild><%= el.Value %></DifferentChild> )
Dim sb As StringBuilder = New StringBuilder()
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.OmitXmlDeclaration = True
xws.Indent = True
Using xw As XmlWriter = XmlWriter.Create(sb, xws)
dstTree.WriteTo(xw)
End Using
Console.WriteLine(sb.ToString())
该示例产生下面的输出:
<NewRoot>
<DifferentChild>3</DifferentChild>
</NewRoot>