Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I have a set of xml data , which is coming from database, and im binding that xml to xslt, bt where im struck is, i wanted to add xslt returned output to html element like div, table etc.

C#
protected string RenderFolderBasedTree()
		{
                        // I wanted to create TextWriter or somnething related this. what would be better if use someother instead of textWriter.. need to return 
			TextWriter output = File.CreateText("C:\\perl.txt");
			StringBuilder sb = new StringBuilder();
			sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			sb.Append("<Folder FolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQA0z9duLrAlQZAgxeM+D8i3AAAA6LgTAAA=\" DisplayName=\"Inbox\" ParentFolderId=\"None\">");
			sb.Append("<Folder expanded=\"false\" FolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyFAAA=\" DisplayName=\"Test1\" ParentFolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQA0z9duLrAlQZAgxeM+D8i3AAAA6LgTAAA=\">");
			sb.Append("<Folder FolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyIAAA=\" DisplayName=\"Test1-1\" ParentFolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyFAAA=\" >");
			sb.Append("</Folder>");
			sb.Append("<Folder FolderId=\"testAAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyIAAA=\" DisplayName=\"Test1-2\" ParentFolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyFAAA=\" >");
			sb.Append("</Folder>");
			sb.Append("</Folder>");
			sb.Append("<Folder expanded=\"false\" FolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyGAAA=\" DisplayName=\"Test2\" ParentFolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQA0z9duLrAlQZAgxeM+D8i3AAAA6LgTAAA=\">");
			sb.Append("<Folder FolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVHTFyFAAA=\" DisplayName=\"Test2-1\" ParentFolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyGAAA=\" ></Folder>");
			sb.Append("<Folder FolderId=\"TestADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVHTFyFAAA=\" DisplayName=\"Test2-2\" ParentFolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyGAAA=\" >");
			sb.Append("<Folder FolderId=\"Test$AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVHTFyFAAA=\" DisplayName=\"Test2-2-1\" ParentFolderId=\"TestADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVHTFyFAAA=\" ></Folder></Folder>");
			sb.Append("</Folder>");
			sb.Append("<Folder expanded=\"false\" FolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQCfuv2rnteuQ512SPTH2nNRAAVGqlyHAAA=\" DisplayName=\"Test3\" ParentFolderId=\"AAMkADljNDMxMTY3LTI3NzYtNDc5Ny05ODI0LThhZTk3Yzc2MWU3MwAuAAAAAABtfBYgWY8sTr4MGI3HqzHtAQA0z9duLrAlQZAgxeM+D8i3AAAA6LgTAAA=\" >");
			sb.Append("</Folder>");
			sb.Append("</Folder>");

			// load stylesheet
			XslCompiledTransform oXSL = new XslCompiledTransform();
			XsltArgumentList args = new XsltArgumentList();
			args.AddParam("Tree.Alt.Expand", string.Empty, AppResourceManager.Default.GetString("Tree.Alt.Expand"));
			args.AddParam("Tree.Alt.Collapse", string.Empty, AppResourceManager.Default.GetString("Tree.Alt.Collapse"));
			args.AddParam("RTL", string.Empty, CrmCultureInfo.CurrentUICulture.TextInfo.IsRightToLeft);
			args.AddParam("id", string.Empty, this.ID);
			XmlDocument xslDocument = XmlUtil.LoadXmlDocumentFromFileId(ApplicationFileId.FolderBasedTrackingTreeXsl);
			oXSL.Load(xslDocument, null, null);

			// transform tree data
			using (StringReader reader = new StringReader(sb.ToString()))
			{
				oXSL.Transform(XmlReader.Create(reader), args, output);
			}
			output.Close();
                        // Returning xslt data to desing page where this actual method is called.
			return output.ToString();
		}


calling above method from html page.
HTML
<tr>
                                    <td>
										<%=RenderFolderBasedTree() %>
                                        
                                    </td>
                                </tr>



Please let me know anything wrong in this code

Many thanks,
-Sanjeev
Posted
Updated 5-Nov-14 22:10pm
v5
Comments
George Jonsson 6-Nov-14 4:02am    
Does the code execute without errors?
Sanjeev Alamuri 6-Nov-14 4:08am    
yeah excuted successfully, and the output saved in perl.txt file, instead of saving in that text file, i just wanted to append to TR.
George Jonsson 6-Nov-14 5:32am    
Please reply to the question instead of adding a new comment, otherwise I will not be notified.

What do you mean by TR? Table Row in HTML?
You want to insert HTML into the page you are calling from?

First, you need to compile your XSLT, and then you can apply it to any XML. This is done using the class System.Xml.Xsl.XslCompiledTransform:
http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform%28v=vs.110%29.aspx[^].

Pay attention for the methods System.Xml.Xsl.XslCompiledTransform.Transform. Basically, you can put the result of transformation to any file, or to some instance of System.Xml.XmlWriter:
http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter(v=vs.110).aspx[^].

That said, it's convenient to have your target HTML (div or whatever else) written by the same instance of the writer. Your question assumes that you will be able to do that.

—SA
 
Share this answer
 
Comments
Sanjeev Alamuri 6-Nov-14 3:58am    
can u please have look at my updated question, i followed through ur solution. bt no data returning form method. and can u give me sample of crating instance of HTML to textwriter?
Sergey Alexandrovich Kryukov 6-Nov-14 10:37am    
That's rigth, no data is returned. The data is written to the file of an XmlWriter you supply. This is what you asked for in first place.
—SA
This is a good place to start if you need information for how to create the XSLT file itself. XSLT Tutorial[^]
The tutorial is about how to convert XML to HTML using XSLT.
You can use Visual Studio to edit and debug your XSLT.

When you have a proper XSLT file, you can use the methods in solution 1 to do the conversion in a c# application.
 
Share this answer
 
v3
simply added below code

C#
StringWriter results = new StringWriter();
			using (XmlReader reader = XmlReader.Create(new StringReader(sb.ToString())))
			{
				oXSL.Transform(reader, null, results);
			}
			return results.ToString();



it is return the data in string format

so solved my prblm :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900