C#/ XSLT:线性化XML部分工作代码
输入XML:
Reported By: L & AQ TESTING, TESTED Date of TESTING: Available TESTING unavailable to resolve Test issue. Additional Comments: Comments Had to go into Testing System and change to the correct notification group. Per sup.
XSLT代码:
预期产量:
Reported By: L & AQ TESTING, TESTED Date of TESTING: Available TESTING unavailable to resolve Test issue. Additional Comments: Comments Had to go into Testing System and change to the correct notification group. Per sup.
我得到了什么:
Reported By: L & AQ TESTING, TESTED Date of TESTING: Available TESTING unavailable to resolve Test issue. Additional Comments: Comments Had to go into Testing System and change to the correct notification group. Per sup.
观察:
尽管来自text()节点的不必要的空格已被纠正。但输出XML中仍有缩进。
理想情况下, strip-space
应该照顾它..在它之上我添加了下面的代码
还是没有运气!! 用法
XPathDocument xpathXmlOrig = new XPathDocument(string_xmlInput);
在我的C#代码错误中说..条带空间不能应用于已经加载的文件!! 所以我使用XMLReader ..
添加C#代码以供参考..
XslCompiledTransform xslTransform = new XslCompiledTransform(); string xslinput = ""; string strXmlOutput = string.Empty; StringWriter swXmlOutput = null; MemoryStream objMemoryStream = null; swXmlOutput = new StringWriter(); objMemoryStream = new MemoryStream(); UTC_Calc obj = new UTC_Calc(); XsltArgumentList xslArg = new XsltArgumentList(); .......... ........ XmlReader reader = XmlReader.Create(string_xmlInput, settings); XsltSettings xslsettings = new XsltSettings(false, true); MemoryStream memStream = new MemoryStream(); XmlReader rd = XmlReader.Create(new StringReader(xslinput)); xslTransform.Load(rd); xslTransform.Transform(reader, xslArg, objMemoryStream); objMemoryStream.Position = 0; StreamReader objStreamReader = new StreamReader(objMemoryStream); strXmlOutput = objStreamReader.ReadToEnd(); .......... ........ XmlDocument outputxml = new XmlDocument(); outputxml.LoadXml(strXmlOutput); outputxml.Save(outputfile.FileName);
您是否可以浏览代码,并查找您为写入流提供的任何XmlWriterSettings
? 仔细检查它是否使用了缩进的输出选项。
如果没有那样的东西,可能显式传入XmlWriterSettings
声明不应该发生格式化将解决这个问题。
XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = false; /* .... */ var outWriter = XmlWriter.Create(outputstream, settings);
http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx
用过的
TextWriter writer = File.CreateText(outputfile.FileName); writer.Write(strXmlOutput); writer.Close();
而不是现有的代码:
XmlDocument outputxml = new XmlDocument(); outputxml.LoadXml(strXmlOutput); outputxml.Save(outputfile.FileName);
我想我也理解了它的区别……
XmlDocument( outputxml.LoadXml, outputxml.Save
),默认情况下强制缩进..虽然strXmlOutput
没有任何缩进..
我得到了这个修复,引用了Jake Heidt的回答。将其标记为社区维基,因为这不是我自己的答案。 此答案也可以使用更有用的信息进行编辑(如果需要)