创建XMLTextReader,但XslCompiledTransform.Transform失败,字符无效 [英] XMLTextReader is created but XslCompiledTransform.Transform fails with invalid character
问题描述
我的代码:
using (XmlTextReader inputReader = new XmlTextReader(xml, XmlNodeType.Document, new XmlParserContext(null, null, "en", XmlSpace.Default)))
{
XsltArgumentList arglist = new XsltArgumentList();
GetXSLT().Transform(inputReader, arglist, outputStream);
}
XmlTextReader
创建良好,在XML内有一个用于垂直制表符(& #xB;
)
The XmlTextReader
is created fine, inside the XML there is an entity reference for a vertical tab (
)
出错的行是对Transform的调用.它说有一个无效的XML字符(当然是垂直标签).
The line that errors is the call to Transform. It says that there is an invalid XML character (the vertical tab of course).
我尝试使用以下文章中引用的方法:
在C#中转义无效的XML字符
I've tried using the approach referenced in the following article:
Escape invalid XML characters in C#
我的问题是:如何使用.NET框架(如链接状态)删除或忽略无效字符?
My question is: how can I remove or ignore the invalid characters using the .NET framework like the link states?
注意:无需硬编码要替换的实体引用列表(我已经在这样做了,这太可怕了,我感到很不舒服,我应该这样做)
note: in a way that doesn't involve hard coding a list of entity references to replace (I'm already doing this and it is horrible and I feel bad, and I should)
推荐答案
尝试在读写时忽略无效的XML字符:
Try ignoring invalid XML characters both while reading and writing:
var readerSettings = new XmlReaderSettings() { CheckCharacters = false, ConformanceLevel = ConformanceLevel.Document };
using (var inputReader = XmlTextReader.Create(xml, readerSettings, new XmlParserContext(null, null, "en", XmlSpace.Default)))
{
XsltArgumentList arglist = new XsltArgumentList();
var xslt = GetXSLT();
var writerSettings = xslt.OutputSettings.Clone();
writerSettings.CheckCharacters = false;
using (var outputWriter = XmlWriter.Create(outputStream, writerSettings))
{
xslt.Transform(inputReader, arglist, outputWriter);
}
}
这篇关于创建XMLTextReader,但XslCompiledTransform.Transform失败,字符无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!