开发者

How to transform XMLDocument using XSLT in C# 2.0

开发者 https://www.devze.com 2023-03-22 11:33 出处:网络
I am using C# 2.0 and I have got below code: XmlDocument doc = new XmlDocument(); doc.LoadXml(GetListOfPagesInStructureGroup(m_Page.Id));

I am using C# 2.0 and I have got below code:

  XmlDocument doc = new XmlDocument();
  doc.LoadXml(GetListOfPagesInStructureGroup(m_Page.Id));

In开发者_开发技巧 above I am loading my XMLDocument with method which returns as string, now after some processing on above xmldocument I want to apply XSLT on the above XMLDocument to render my desired result according to XSLT and finally my function will return whole rendered XML as string

Please suggest!!


Please suggest on below solution:

        XslCompiledTransform xslTransform = new XslCompiledTransform();
        StringWriter writer = new StringWriter();          
        xslTransform.Load("xslt/RenderDestinationTabXML.xslt");
        xslTransform.Transform(doc.CreateNavigator(),null, writer);
        return writer.ToString();

Thanks!!


Try the XslCompiledTransform class.


There are lots of examples on the web of transforming an XML file to a different format using an XSLT file, like the following:

XslTransform myXslTransform = new XslTransform();
XsltSettings myXsltSettings = new XsltSettings();
myXsltSettings.EnableDocumentFunction = true;
myXslTransform.Load("transform.xsl");
myXslTransform.Transform("input.xml", "output.xml");

However this is only a partial answer, I would like to be able to get the XML input data from a web form and use that as the input data instead of an '.xml' file, but have not found any concrete examples, also using Visual Studio I can see the different constructors and methods that are available and I am not seeing one that accepts xml data in a string format, so it would be very helpful if someone could provide an example of that.


Re " I want my same XMlDocument updated " - it doesn't work like that; the output is separate to the input. If that is important, just use a StringWriter or MemoryStream as the destination, then reload the XmlDocument from the generated output.

Consider in particular: the output from an xslt transformation does not have to be xml, and also: the xslt is most likely using the node tree during the operation; changing the structure in-place would make that very hard.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号