[英]Parsing XmlTextWriter object to String
I'm building a class library in C# which uses the XmlTextWriter class to build an XML which is then exported as a HTML document.
但是,当我使用 XmlTextWriter object 作为内容保存扩展名为 .html 的文件时,生成的文件仅包含文本“System.Xml.XmlTextWriter”
这出现在下面定义的方法中,特别是在最后一行:-
public void SaveAsHTML(string filepath)
{
XmlTextWriter html;
html = new XmlTextWriter(@"D:/HTMLWriter/XML/HTMLSaveAsConfig.xml", System.Text.Encoding.UTF8);
html.WriteStartDocument();
html.WriteStartElement("html");
html.WriteRaw(Convert.ToString(Head));
html.WriteRaw(Convert.ToString(Body));
html.WriteEndElement();
html.WriteEndDocument();
html.Flush();
html.Close();
System.IO.File.WriteAllText(filepath, html.ToString());
}
对于上下文,变量 Head 和 Body 也是 XmlTextWriter 对象,分别包含将成为 html 文件的元素的内容。
我试过使用 Convert.ToString(),这会导致同样的问题。
我很想尝试覆盖我的 class 的 ToString() 方法作为修复,可能使用 XmlSerializer class。 但是我想知道是否有一种噪音较小的方式将 Xml object 作为字符串返回?
以下 function 将从您创建为Head
和Body
的System.Xml.XmlTextWriter
对象中提取字符串。
private string XmlTextWriterToString(XmlTextWriter writer)
{
// Ensure underlying stream is flushed.
writer.Flush();
// Reset position to beginning of stream.
writer.BaseStream.Position = 0;
using (var reader = new StreamReader(writer.BaseStream))
{
// Read and return content of stream as a single string
var result = reader.ReadToEnd();
return result;
}
}
Some caveats here are that the underlying System.IO.Stream
object associated with the System.Xml.XmlTextWriter
must support both 'read' and 'seek' operations (ie, both Stream.CanRead
and System.CanSeek
properties must return true
, respectively) .
我对您的原始代码进行了以下编辑:
Convert.ToString
()调用替换为对这个新 function 的调用。filepath
参数指定的文件写入您的SaveAsHTML
() function,而不是硬编码路径。System.Xml.XmlTextWriter
的创建(以及使用和处置)包装在using
块中(如果您不熟悉,请参阅C# 中“使用”的用途是什么? )。以下是您进行这些更改的代码。
public void SaveAsHTML(string filepath)
{
using (var html = new XmlTextWriter(filepath, System.Text.Encoding.UTF8))
{
html.WriteStartDocument();
html.WriteStartElement("html");
html.WriteRaw(XmlTextWriterToString(Head));
html.WriteRaw(XmlTextWriterToString(Body));
html.WriteEndElement();
html.WriteEndDocument();
html.Flush();
html.Close();
}
}
需要注意的另一件事是,从提供的代码中无法确定如何管理它们, Head
和Body
的生命周期受到与html
在将其包装在using
块。
最后的想法: System.Xml.XmlTextWriter
的页面注释如下:从 .NET Framework 2.0 开始,我们建议您使用 XmlWriter.Create 方法和 XmlWriterSettings ZA2F2ED4F8EBC2CBB4ZC21 的新功能创建 XmlWriter 实例
最后一行写入 XmlTextWriter.ToString() 的值,它不会返回您编写的 XML 的文本表示形式。 尝试离开最后一行,看起来您的 XmlTextWriter 已经在写入文件。
@PhilBrubaker 的解决方案似乎在正确的轨道上。 我的代码中仍然存在一些错误,我正在努力修复,但好消息是铸造现在似乎正在工作。
protected string XmlToString(XmlWriter xmlBody)
{
XmlTextWriter textXmlBody = (XmlTextWriter)xmlBody;
textxmlBody.BaseStream.Position = 0;
using (var reader = new StreamReader(textXmlBody.BaseStream))
{
var result = reader.ReadToEnd();
reader.Dispose();
return result;
}
}
我已将输入参数类型从 XmlWriter 更改并在方法中将其显式转换为 XmlTextWriter,这样当使用 Create() 方法而不是 .NET 2.0 推荐的初始化时,该方法也可以工作。 目前它不是 100% 可靠的,因为 XmlWriter 并不总是正确地转换为 XmlTextWriter(取决于功能),但这不在此线程的 scope 范围内,我正在单独调查。
谢谢你的帮助!
附带说明一下, using 块是我以前没有遇到过的,但它为我提供了很多全面的解决方案。 所以也谢谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.