我正在用 C# 编写一个 Windows 服务。我有一个 XmlWriter
,它包含 XSLT 转换的输出。我需要将 XML 放入 XMLElement
对象中以传递给 Web 服务。
执行此操作的最佳方法是什么?
最佳答案
您不需要中间字符串,您可以创建一个直接写入 XmlNode 的 XmlWriter:
XmlDocument doc = new XmlDocument();
using (XmlWriter xw = doc.CreateNavigator().AppendChild()) {
// Write to `xw` here.
// Nodes written to `xw` will not appear in the document
// until `xw` is closed/disposed.
}
并将 xw 作为转换的输出传递。
注意。 xsl:output 的某些部分将被忽略(例如编码),因为 XmlDocument 将使用其自己的设置。
关于c# - 在 C# 中从 XML Writer 创建 XML 元素对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/560842/