Passing XmlResolver to Transform() method
-
Hi, I have a method to render HTML from XSLT. Here is the method: /// /// Method that renders HTML from XSLT document /// /// protected override void Render(HtmlTextWriter writer) { XPathDocument xdoc = new XPathDocument(Context.Server.MapPath(sourceFilePath)); XslTransform xslt = new XslTransform(); xslt.Load(Context.Server.MapPath(transformFilePath)); xslt.Transform(xdoc, null, writer); } I am now getting this warning: warning CS0618: 'System.Xml.Xsl.XslTransform.Transform(System.Xml.XPath.IXPathNavigable, System.Xml.Xsl.XsltArgumentList, System.IO.TextWriter)' is obsolete: 'You should pass XmlResolver to Transform() method' I need some help resolving this warning. Can you help? Kyle
-
Hi, I have a method to render HTML from XSLT. Here is the method: /// /// Method that renders HTML from XSLT document /// /// protected override void Render(HtmlTextWriter writer) { XPathDocument xdoc = new XPathDocument(Context.Server.MapPath(sourceFilePath)); XslTransform xslt = new XslTransform(); xslt.Load(Context.Server.MapPath(transformFilePath)); xslt.Transform(xdoc, null, writer); } I am now getting this warning: warning CS0618: 'System.Xml.Xsl.XslTransform.Transform(System.Xml.XPath.IXPathNavigable, System.Xml.Xsl.XsltArgumentList, System.IO.TextWriter)' is obsolete: 'You should pass XmlResolver to Transform() method' I need some help resolving this warning. Can you help? Kyle
you need to use a XPathNavigator:
XslTransform xslt = new XslTransform(); XmlDocument formDocument = new XmlDocument(); sourceDocument.Load(sourceFileName); formDocument.Load(formFileName); xslt.Load(stylesheetFileName); MemoryStream ms = new MemoryStream(); **XPathNavigator nav = formDocument.CreateNavigator();** xslt.Transform(nav, null, ms, null); StreamReader sr = new StreamReader(ms); ms.Position = 0; string result = sr.ReadToEnd(); sr.Close();
"When the only tool you have is a hammer, a sore thumb you will have."
-
you need to use a XPathNavigator:
XslTransform xslt = new XslTransform(); XmlDocument formDocument = new XmlDocument(); sourceDocument.Load(sourceFileName); formDocument.Load(formFileName); xslt.Load(stylesheetFileName); MemoryStream ms = new MemoryStream(); **XPathNavigator nav = formDocument.CreateNavigator();** xslt.Transform(nav, null, ms, null); StreamReader sr = new StreamReader(ms); ms.Position = 0; string result = sr.ReadToEnd(); sr.Close();
"When the only tool you have is a hammer, a sore thumb you will have."
Thank you Nick! I did need to use an XPathNavigator but even then the error message about using XmlResolver still appeared. Upon further research I found that XmlResolver is a argument in the Transform method. Transform(XPathNavigator, XsltArgumentList, TextWriter, XmlResolver); I had left this argument out and that is what generated the error. I still have no clue as to when or why to use a XmlResolver but found when I inserted a null for the arguemnt as it appears in your code that the error disappeared. Transform(nav,null,writer,null); I suspect you can create custom resolvers to influence the Transform but that's for another day. Problem solved. Thank you. Kyle