How to convert List & amp; lt; T & amp; gt; into Html (xsl dynamic conversion) (2)

Source: Internet
Author: User

I. Preface
Followed by the previous article, this article mainly involves how to dynamically convert xml and xsl into html. This is the most important thing. All the content is carried out around this topic. The List <T> is converted to the corresponding Html according to the specified xsl style. The related content is as follows:

(1) convert List to DataTable.

(2) dynamically convert Xml and Xsl into Html.

(3) set and control the content style of Xsl.

 

II. Specific implementation of terratransform

Transform transform's main responsibilities are to dynamically convert xml and xsl into html. XslCompiledTransform provides many methods for conversion, but it is not very convenient to use. Therefore, you must encapsulate and extract the current class based on the method. The specific code is as follows:

1 public class implements transform: IDisposable
2 {
3 public StringWriter
4 {
5 get;
6 private set;
7}
8
9 public StringReader XmlStringReader
10 {
11 get;
12 private set;
13}
14
15 public StringReader extends StringReader
16 {
17 get;
18 private set;
19}
20
21 public writable transform ()
22 {
23 this. StringWriter = new StringWriter ();
24}
25
26 public string Transfer (string xmlText, string plain text)
27 {
28 if (string. IsNullOrWhiteSpace (xmlText)
29 | string. IsNullOrWhiteSpace (plain text ))
30 {
31 return string. Empty;
32}
33 this. XmlStringReader = new StringReader (xmlText );
34 this. Lost StringReader = new StringReader (plain text );
35
36 return TransferXmlAndXslToHtml ();
37}
38
39 private string TransferXmlAndXslToHtml ()
40 {
41 try
42 {
43 using (XmlTextWriter writer = new XmlTextWriter (this. StringWriter ))
44 {
45 return ToHtml (writer );
46}
47}
48 catch
49 {
50 return string. Empty;
51}
52}
53
54 private string ToHtml (XmlTextWriter writer)
55 {
56 convert compiledtransform into transform = new convert compiledtransform ();
57 convert transform. Load (XmlReader. Create (this. extends stringreader ));
58 convert Transform. Transform (XmlReader. Create (this. XmlStringReader), writer );
59
60 return this. StringWriter. ToString ();
61}
62
63 # region IDisposable Member
64
65 public void Dispose ()
66 {
67 this. Dispose (true );
68 GC. SuppressFinalize (this );
69}
70
71 private void Dispose (bool disposing)
72 {
73 if (this. StringWriter! = Null)
74 {
75 this. StringWriter. Dispose ();
76}
77 if (this. Repeated stringreader! = Null)
78 {
79 this. inclustringreader. Dispose ();
80}
81 if (this. XmlStringReader! = Null)
82 {
83 this. XmlStringReader. Dispose ();
84}
85}
86
87 # endregion
88}
 

The main method of the terratransform class is the code of lines 54-61, as shown below:

54 private string ToHtml (XmlTextWriter writer)
55 {
56 convert compiledtransform into transform = new convert compiledtransform ();
57 &

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.