我目前正在使用以下代码
运行实时“内存中”XSLT转换XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(DS.GetXml());
XslCompiledTransform XsltTranformation = new XslCompiledTransform();
XsltTranformation.Load(@"C:\Users\maskew\Desktop\XSLTMapping.xsl");
Stream XmlStream = new MemoryStream();
XmlDoc.Save(XmlStream); //Stream is still blank after this line
XmlReader XmlRdr = XmlReader.Create(XmlStream);
MemoryStream stm = new MemoryStream();
XsltTranformation.Transform(XmlRdr, null, stm);
stm.Position = 1;
StreamReader sr = new StreamReader(stm);
string Output = sr.ReadToEnd();
Output = Output.Substring(2);
XmlDoc.LoadXml(Output);
XmlWriter XmlWrtr = XmlWriter.Create(@"C:\Users\maskew\Desktop\XmlMapping.xml");
XmlDoc.WriteTo(XmlWrtr);
XmlWrtr.Flush();
XmlWrtr.Close();
但是,当我将文件从XmlDocument移动到第6行的MemoryStream时,流检查时不包含任何内容,从而停止整个程序的运行。
有谁知道为什么会这样?
更新:流现在包含信息,但是XmlReader对象仍然没有收到任何信息。
答案 0 :(得分:2)
尝试简化
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.LoadXml(DS.GetXml());
// Create a writer for writing the transformed file.
XmlWriter writer = XmlWriter.Create(@"C:\Users\maskew\Desktop\XmlMapping.xml");
// Create and load the transform with script execution enabled.
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableScript = true;
transform.Load(@"C:\Users\maskew\Desktop\XSLTMapping.xsl", settings, null);
// Execute the transformation.
transform.Transform(xmlDoc, writer);
答案 1 :(得分:1)
Stream XmlStream = new MemoryStream();
怎么会有东西?你正在构建一个空的memoryStream ......
编辑:
您应该尝试使用“using”指令(http://msdn.microsoft.com/fr-fr/library/yh598w02)来澄清它。基本上,像StreamReader,MemeryStream等类实现IDisposable接口。如果使用它来包装它们,它将自动为您配置对象。
答案 2 :(得分:1)
尝试刷新并重置流:
XmlDoc.Save(XmlStream);
XmlStream.Flush();
XmlStream.Position = 0;
XmlReader XmlRdr = XmlReader.Create(XmlStream);