Jak zastosować arkusz stylów XSLT w C#
Chcę zastosować arkusz stylów XSLT do dokumentu XML za pomocą C# i zapisać wyjście do pliku.
3 answers
Znalazłem możliwą odpowiedź tutaj: http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63
Z Artykułu:
XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;
myXslTrans.Transform(myXPathDoc,null,myWriter) ;
Edit:
Ale mój zaufany kompilator mówi, {[2] } jest przestarzały: Użyj XslCompiledTransform
zamiast:
XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null);
myXslTrans.Transform(myXPathDoc,null,myWriter);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-02 12:38:28
Bazując na doskonałej odpowiedzi Darena, zauważ, że ten kod można znacznie skrócić za pomocą odpowiedniego XslCompiledTransform.Transform overload :
var myXslTrans = new XslCompiledTransform();
myXslTrans.Load("stylesheet.xsl");
myXslTrans.Transform("source.xml", "result.html");
(Przepraszam, że podaję to jako odpowiedź, ale poparcie code block
w komentarzach jest raczej ograniczone.)
W VB.NET, nie potrzebujesz nawet zmiennej:
With New XslCompiledTransform()
.Load("stylesheet.xsl")
.Transform("source.xml", "result.html")
End With
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-14 09:11:26
Oto tutorial Jak zrobić transformacje XSL w C# na MSDN:
Http://support.microsoft.com/kb/307322/en-us/
A tutaj jak pisać Pliki:
Http://support.microsoft.com/kb/816149/en-us
Tak na marginesie: jeśli chcesz zrobić walidację również tutaj jest inny samouczek (dla DTD, XDR i XSD (=Schema)):
Http://support.microsoft.com/kb/307379/en-us/
Dodałem to tylko po to, aby zapewnić więcej informacje.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-26 10:55:03