我需要使用XSLT转换HTML网页的内容。因此,我使用了SgmlReader并编写了如下所示的片段(我认为,最终它也是一个XmlReader .)
XmlReader xslr = XmlReader.Create(new StringReader(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" +
"<xsl:output method=\"xml\" encoding=\"UTF-8\" version=\"1.0\" />" +
"<xsl:template match=\"/\">" +
"<XXX xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><xsl:value-of select=\"count(//br)\" /></XXX>" +
"</xsl:template>" +
"</xsl:stylesheet>"));
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xslr);
using (SgmlReader html = new SgmlReader())
{
StringBuilder sb = new StringBuilder();
using (TextWriter sw = new StringWriter(sb))
using (XmlWriter xw = new XmlTextWriter(sw))
{
html.InputStream = new StringReader(Resources.html_orig);
html.DocType = "HTML";
try
{
xslt.Transform(html, xw);
string output = sb.ToString();
System.Console.WriteLine(output);
}
catch (Exception exc)
{
System.Console.WriteLine("{0} : {1}", exc.GetType().Name, exc.Message);
System.Console.WriteLine(exc.StackTrace);
}
}
}尽管如此,我还是得到了错误消息。
NullReferenceException : Object reference not set to an instance of an object.
at MS.Internal.Xml.Cache.XPathDocumentBuilder.Initialize(XPathDocument doc, IXmlLineInfo lineInfo, String baseUri, LoadFlags flags)
at MS.Internal.Xml.Cache.XPathDocumentBuilder..ctor(XPathDocument doc, IXmlLineInfo lineInfo, String baseUri, LoadFlags flags)
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space)
at System.Xml.Xsl.Runtime.XmlQueryContext.ConstructDocument(Object dataSource, String uriRelative, Uri uriResolved)
at System.Xml.Xsl.Runtime.XmlQueryContext..ctor(XmlQueryRuntime runtime, Object defaultDataSource, XmlResolver dataSources, XsltArgumentList argList, WhitespaceRuleLookup wsRules)
at System.Xml.Xsl.Runtime.XmlQueryRuntime..ctor(XmlQueryStaticData data, Object defaultDataSource, XmlResolver dataSources, XsltArgumentList argList, XmlSequenceWriter seqWrt)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
at System.Xml.Xsl.XmlILCommand.Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
at System.Xml.Xsl.XslCompiledTransform.Transform(XmlReader input, XmlWriter results)通过将HTML转换为XML,然后应用转换,我找到了解决这个问题的方法,但这是一个效率低下的解决方案,因为:
所以(因为我知道StackOverflow社区总是提供很好的答案,而其他C#论坛却完全让我失望;o),我将寻找反馈和建议,以便直接使用SgmlReader执行XSL转换(即使SgmlReader需要被另一个类似的库所取代)。
发布于 2010-11-30 15:41:42
即使SgmlReader类正在扩展XmlReader类,但这并不意味着它的行为也类似于XmlReader。
从技术上讲,SgmlReader是XmlReader的一个子类也是没有意义的,因为SGML是一个超集,而不是一个子集。
您没有写过转换的目的,但是一般来说,HTML敏捷包是操作HTML的一个很好的选择。
发布于 2010-11-30 15:26:52
你试过使用HTML敏捷包而不是SgmlReader吗?您可以将html加载到其中,并直接对其运行转换。不过,如果XML文档是在内部创建的,我就不确定了--尽管似乎没有创建XML文档,但您可能希望将内存和CPU的使用情况与您尝试和丢弃的转换方法进行比较。
//You already have your xslt loaded into var xslt...
HtmlDocument doc = new HtmlDocument();
doc.Load( ... ); //load your HTML doc, or use LoadXML from a string, etc
xslt.Transform(doc, xw);也请参阅这个问题:如何使用HTML敏捷包
https://stackoverflow.com/questions/4315195
复制相似问题