Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Using system.io;
USING SYSTEM.XML;
Namespace MyWindows
{
///
/// This example demonstrates how to encode the Office file as an XML file and how to convert the generated XML file into Office files.
/// convert the file into the XML format, then you can transfer it with a web service, .NET Remoting, Winsock, etc. (where the two can be transferred without conversion)
/// XML solves the problem of data transmission in the multi-layer architecture, for example, in the client can use the web service to get the server-end office file, and then return to the server.
// / Just convert the file into the XML format, there is a lot of schemes to be used, and XML has platform independent, you can use .NET to publish web services with .NET, and then use
/// java Write a paragraph APPLIT applet to handle the sending file, of course, there is almost no meaning of the example, but it gives us a lot of revelation.
/// The other if your solution is based on a multi-platform, the interaction between them is best not to call (RPC) with a remote application interface (RPC) should try to use document-based interactions.
///, for example, the JMQ of the MSMQ and J2EE under the .NET.
///
/// Example is designed to a lot of classes, I didn't do more comments in all where, please refer to MSDN, this is the first Windows program, there is a place
/// Welcome to guide
/// summary>
Public Class Form1: System.Windows.Forms.form
{
///
/// Declare four button, an OpenFileDialog, a SaveFileDialog, and two xmldocument
/// summary>
Private system.windows.Forms.Button button1;
Private system.windows.Forms.Button Button2;
Private system.windows.forms.openfiledialog openfiledialog1;
Private system.windows.forms.savefiledialog savefiledialog1;
Private system.windows.Forms.Button Button3;
Private system.windows.Forms.Button Button4;
Private system.xml.xmldocument mxmldoc;
Private system.xml.xmldocument doc;
Private system.componentmodel.Container Components = NULL;
Public Form1 ()
{
//
// Windows Form Designer Support
//
InitializationComponent ();
//
// Todo: Add any constructor code after INITIALIZECOMPONENT call
//
}
///
/// Clean all the resources being used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
This.button1 = new system.windows.Forms.Button ();
This.Button2 = new system.windows.Forms.Button ();
This.openfileDialog1 = new system.windows.Forms.openFiledialog ();
THIS.SAVEFILEDIALOG1 = New System.windows.Forms.savefileDialog ();
This.Button3 = new system.windows.Forms.Button ();
This.button4 = new system.windows.Forms.Button ();
THIS.SUSPENDLAYOUT ();
//
// Button1
//
This.button1.location = new system.drawing.point (96, 32);
This.button1.name = "button1";
this.button1.tabindex = 0;
This.button1.text = "Generate XML";
This.Button1.click = new system.eventhandler (this.button1_click);
//
// Button2
//
This.button2.location = new system.drawing.point (96, 80);
This.Button2.name = "button2";
This.button2.tabindex = 1;
This.button2.text = "Generate DOC";
This.button2.click = new system.eventhandler (this.button2_click);
//
// Button3
//
This.Button3.Location = new system.drawing.point (8, 32);
This.button3.name = "button3";
This.button3.tabindex = 2;
This.Button3.Text = "Load DOC";
This.Button3.click = new system.eventhandler (this.button3_click);
//
// Button4
//
This.Button4.Location = new system.drawing.point (8, 80);
This.button4.name = "button4";
This.button4.tabindex = 3;
this.button4.text = "Load XML";
This.button4.click = new system.eventhandler (this.button4_click);
//
// Form1
//
THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);
THIS.CLIENTSIZE = New System.drawing.size (184, 141);
This.Controls.add (this.button4);
This.Controls.add (this.button3);
This.Controls.add (this.button2);
This.Controls.add (this.button1);
THIS.NAME = "Form1";
THIS.TEXT = "Form1";
This.ResumeLayout (false);
//
// Register your LOAD and CLOSED events manually
//
This.Load = New System.EventHandler (this.form1_load);
this.closed = new system.eventhandler (this.form1_closed);
}
#ndregion
///
/// Start the form from this entrance
/// summary>
Static void main ()
{
Application.run (New Form1 ());
}
///
// / Convert the loaded office file to an XML file
/// summary>
/// param>
/// param>
Private void Button1_Click (Object Sender, System.Eventargs E)
{
SavefileDialog1.filter = "XML file | * .xml"; // Set the file filtering condition to open the dialog
SaveFileDialog1.title = "Save into XML file"; // Set the title of the open dialog
SavefileDialog1.filename = "";
SavefileDialog1.showdialog (); // Open dialog
if (SaveFileDialog1.FileName! = "") // Detects whether the user has entered a saved file name
{
MXMLDoc.save (SavefileDialog1.FileName); // Save file with private objects, MXMLDOC is declared in front
Messagebox.show ("Save Success");
}
}
///
// / convert the loaded XML file into Office file
/// summary>
/// param>
/// param>
Private void button2_click (Object Sender, System.Eventargs E)
{
// Select the ME node from the private object DOX, some of the operation of the XML object, detail, can refer to MSDN for more information
XMLNode node = doc.documentelement .selectsinglenode ("me");
XMLELEMENT ELE = (XMLELEMENT) Node; / / Get an XML element
String pic = ele.getattribute ("aa"); // Gets the AA property of the ELE element and reports in a temporary string variable PIC
Byte [] bytes = convert.frombase64string (pic); // Declare a BYTE [] used to store the base64 decoded data stream
// Get file save address from the Save Dialog SaveFileDialog1.filter = "Office Documents (*. DOC, * .XLS, * .PPT) | * .doc; *. Xls; *. Ppt"
SavefileDialog1.title = "Save into Office File";
SavefileDialog1.filename = "";
SavefileDialog1.showdialog ();
IF (SavefileDialog1.FileName! = "")
{
// Create a file stream and save
FileStream outfile = new system.io .filestream (SavefileDialog1.FileName, System.IO.FileMode.createNew);
Outfile.write (bytes, 0, (int) bytes.length);
Messagebox.show ("Save Success");
}
}
///
// Some initialization behavior when loading the window
/// summary>
/// param>
/// param>
Public Void Form1_Load (Object Sender, System.EventArgs E)
{
Messagebox.show ("Welcome Frog Fans Document Converter");
}
///
/// Release the temporary variable when uninstalling the form
/// summary>
/// param>
/// param>
Public void form1_closed (Object Sender, System.EventArgs E)
{
mxmldoc = null;
DOC = NULL;
}
///
/// Load the Office file and encode the sequence flower is an XMLDocument variable
/// summary>
/// param>
/// param>
Private void button3_click (Object Sender, System.Eventargs E)
{
String strfilename;
OpenfileDialog1.filter = "Office documents (*. doc, * .xls, * .ppt) | * .doc; *. xls; *. ppt"
OpenFiledialog1.FilterIndex = 1;
OpenFileDialog1.FileName = "";
OpenFileDialog1.showdialog ();
Strfilename = OpenFileDialog1.FileName;
IF (strfilename.length! = 0)
{
System.IO.FileStream Infile = New fileStream (StrfileName, System.IO.Filemode.Open, System.IO.FileAccess.read);
Byte [] binarydata = new byte [infile.length]; infile.read (binarydata, 0, (int) infile.length);
String mstr = convert.tobase64string (binarydata);
String hh = mstr;
MXMLDoc = new system.xml.xmldocument ();
MSTR = string.format ("
MXMLDoc.loadxml (MSTR);
Messagebox.show ("loading success");
}
}
///
/// Load XML file to private object DOX
/// summary>
/// param>
/// param>
Private void button4_click (Object Sender, System.Eventargs E)
{
String strfilename;
OpenFileDialog1.filter = "XML file | * .xml";
OpenFiledialog1.FilterIndex = 1;
OpenFileDialog1.FileName = "";
OpenFileDialog1.showdialog ();
Strfilename = OpenFileDialog1.FileName;
// if the user does not cancel, Open the document.
IF (strfilename.length! = 0)
{
Doc = new xmldocument ();
Doc.Load (StrfileName);
Messagebox.show ("loading success");
}
}
}
}