XML StringWriter 空
我在 XMLDocument
中有一些 XML,我想将其放入 StringWriter
中,但它总是变成空(?)。我尝试过使用调试检查变量,但我仍然不知道发生了什么。
这是我所拥有的:
XmlDocument xmlInput = new XmlDocument();
/* after this executes, xmlInput has lots of data in it */
xmlInput.LoadXml(...);
XslCompiledTransform transform = new XslCompiledTransform();
/* I'm pretty sure Template.xslt is being found and loaded correctly */
transform.Load(Server.MapPath("/Files/Template.xslt"));
using (System.IO.StringWriter sb = new System.IO.StringWriter())
{
XmlWriterSettings xSettings = new XmlWriterSettings();
xSettings.ConformanceLevel = ConformanceLevel.Fragment;
xSettings.Encoding = Encoding.UTF8;
/* PROBLEM: After this line, StringWriter sb is {} */
using (XmlWriter xWriter = XmlWriter.Create(sb, xSettings))
{
transform.Transform(xmlInput, xWriter);
}
/* xmlText is empty string "" after this line */
String xmlText = sb.ToString();
/* ... does more stuff ... */
}
我可以帮忙吗?我真的不知道从这里到哪里去让它工作。
编辑:更多的调查:
using (var stringWriter = new StringWriter())
{
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
/* Prints lots of data to the screen */
Response.Write(xmlInput.InnerXml);
xmlInput.WriteTo(xmlTextWriter);
/* Doesn't print anything to the screen */
Response.Write(stringWriter.GetStringBuilder().ToString() );
}
}
但是,冲洗就可以了
xmlInput.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
/* prints lots of data */
Response.Write( stringWriter.GetStringBuilder().ToString() );
I have some XML in a XMLDocument
and I want to put it into a StringWriter
, but it always becomes empty(?) instead. I've tried inspecting the variables with debug, but I'm still clueless as to what's going on.
Here's what I have:
XmlDocument xmlInput = new XmlDocument();
/* after this executes, xmlInput has lots of data in it */
xmlInput.LoadXml(...);
XslCompiledTransform transform = new XslCompiledTransform();
/* I'm pretty sure Template.xslt is being found and loaded correctly */
transform.Load(Server.MapPath("/Files/Template.xslt"));
using (System.IO.StringWriter sb = new System.IO.StringWriter())
{
XmlWriterSettings xSettings = new XmlWriterSettings();
xSettings.ConformanceLevel = ConformanceLevel.Fragment;
xSettings.Encoding = Encoding.UTF8;
/* PROBLEM: After this line, StringWriter sb is {} */
using (XmlWriter xWriter = XmlWriter.Create(sb, xSettings))
{
transform.Transform(xmlInput, xWriter);
}
/* xmlText is empty string "" after this line */
String xmlText = sb.ToString();
/* ... does more stuff ... */
}
Can I have some help? I'm really not sure where to go from here to get this working.
EDIT: Even more investigation:
using (var stringWriter = new StringWriter())
{
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
/* Prints lots of data to the screen */
Response.Write(xmlInput.InnerXml);
xmlInput.WriteTo(xmlTextWriter);
/* Doesn't print anything to the screen */
Response.Write(stringWriter.GetStringBuilder().ToString() );
}
}
BUT, flushing does the trick
xmlInput.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
/* prints lots of data */
Response.Write( stringWriter.GetStringBuilder().ToString() );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我尝试应用的 XSLT 文件需要将 XML 包装在某个元素中。
但
flush()
技巧很有帮助。It turns out that the XSLT file I was trying to apply required the XML to be wrapped in a certain element.
But the
flush()
trick helped a lot.