XPathXSLSample4.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:4k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Data;
  8. using System.Xml;
  9. using System.Xml.XPath;
  10. using System.Xml.Xsl;
  11. namespace Wrox.ProCSharp.Xml.XPathXSLSample4
  12. {
  13. /// <summary>
  14. /// Summary description for Form1.
  15. /// </summary>
  16. public class Form1 : System.Windows.Forms.Form
  17. {
  18. private System.Windows.Forms.ListBox listBox1;
  19. private System.Windows.Forms.Button button1;
  20. /// <summary>
  21. /// Required designer variable.
  22. /// </summary>
  23. private System.ComponentModel.Container components=null;
  24. public Form1()
  25. {
  26. //
  27. // Required for Windows Form Designer support
  28. //
  29. InitializeComponent();
  30. //
  31. // TODO: Add any constructor code after InitializeComponent call
  32. //
  33. }
  34. /// <summary>
  35. /// Clean up any resources being used.
  36. /// </summary>
  37. protected override void Dispose( bool disposing )
  38. {
  39. if( disposing )
  40. {
  41. if (components != null) 
  42. {
  43. components.Dispose();
  44. }
  45. }
  46. base.Dispose( disposing );
  47. }
  48. #region Windows Form Designer generated code
  49. /// <summary>
  50. /// Required method for Designer support - do not modify
  51. /// the contents of this method with the code editor.
  52. /// </summary>
  53.     private void InitializeComponent()
  54.     {
  55.       this.listBox1 = new System.Windows.Forms.ListBox();
  56.       this.button1 = new System.Windows.Forms.Button();
  57.       this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  58.         | System.Windows.Forms.AnchorStyles.Right);
  59.       this.listBox1.Size = new System.Drawing.Size(336, 238);
  60.       this.listBox1.TabIndex = 0;
  61.       this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  62.       this.button1.Location = new System.Drawing.Point(136, 264);
  63.       this.button1.TabIndex = 1;
  64.       this.button1.Text = "Load XML";
  65.       this.button1.Click += new System.EventHandler(this.button1_Click);
  66.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  67.       this.ClientSize = new System.Drawing.Size(339, 320);
  68.       this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1,
  69.                                                                   this.listBox1});
  70.       this.Text = "Form1";
  71.     }
  72. #endregion
  73. /// <summary>
  74. ///  The main entry point for the application.
  75. /// </summary>
  76. [STAThread]
  77. static void Main()
  78. {
  79. Application.Run(new Form1());
  80. }
  81.     //XPathXSLSample4/form1.cs
  82.     private void button1_Click(object sender, System.EventArgs e)
  83.     {
  84.       //new XPathDocument
  85.       XPathDocument doc=new XPathDocument("..\..\..\booksxpath.xml");
  86.       //new XslTransform
  87.       XslTransform transForm=new XslTransform();
  88.       transForm.Load("..\..\..\booksarg.xsl");
  89.       //new XmlTextWriter since we are creating a new xml document
  90.       XmlWriter xw=new XmlTextWriter("..\..\..\argSample.xml",null);
  91.       //create the XslArgumentList and new BookUtils object
  92.       XsltArgumentList argBook=new XsltArgumentList();
  93.       BookUtils bu=new BookUtils();
  94.       //this tells the argumentlist about BokUtils
  95.       argBook.AddExtensionObject("urn:ProCSharp",bu);
  96.       //new XPathNavigator
  97.       XPathNavigator nav=((IXPathNavigable)doc).CreateNavigator();
  98.       //do the transform
  99.       transForm.Transform(nav,argBook,xw);
  100.       xw.Close();
  101.       MessageBox.Show("argSample.xml created");
  102.  
  103.     }
  104.     
  105.     //simple test class
  106.     public class BookUtils
  107.     {
  108.       public BookUtils(){}
  109.       
  110.       public string ShowText()
  111.       {
  112.         return "This came from the ShowText method!"; 
  113.       }
  114.     }
  115.     
  116. }
  117. }