Code _c# to convert files into XML in C # tutorial

Source: Internet
Author: User
Tags save file
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.IO;
Using System.Xml;
Namespace MyWindows
{
/**////<summary>
This example shows how to encode an Office file as an XML file and how to convert the resulting XML file into an Office file
Convert the file to XML format, and then you can use the Web service,. NET Remoting,winsock, and so on (in which the latter can be transferred without conversion or transfer)
XML solves the problem of data transfer in multi-tier architectures, such as when clients can get server-side Office files with Web services, and then pass them back to the server
As long as the file into XML format, there are a lot of scenarios can be used, and XML is platform-independent, you can use. NET in the server to publish Web services, and then the client
Java Write a applit small program to deal with the file sent over, of course, I cite the example of almost no meaning, it gave us a lot of inspiration.
In addition, if your solution is based on multiplatform, then the interaction between them is best not to use the remote application interface call (RPC), should try to use the document-based interaction,
For example, MSMQ,J2EE's jmq under. Net.
///
There are a lot of classes designed in the example, I don't have too many comments in all the places, and I don't know where to go. See MSDN, this is my first Windows program, there are some wrong places
You are welcome to guide
</summary>
public class Form1:System.Windows.Forms.Form
{

/**////<summary>
Declare four button, one OpenFileDialog, one 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 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

/**////<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Windows Forms Designer generated Code #region the Windows Forms Designer generated code
/**////<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
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);
//
Manually register the load and closed events
//
This. Load + = new System.EventHandler (this. Form1_Load);
This. Closed + = new System.EventHandler (this. form1_closed);

}
#endregion

/**////<summary>
Start form from this portal
</summary>
static void Main ()
{
Application.Run (New Form1 ());
}
/**////<summary>
Convert the loaded Office file to an XML file
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button1_Click (object sender, System.EventArgs e)
{
Savefiledialog1.filter = "XML file |*.xml";//Set file filter criteria for Open dialog box
Savefiledialog1.title = "Save as XML file";//Set the title of the Open dialog box
Savefiledialog1.filename= "";
Savefiledialog1.showdialog ()//Open dialog box

if (Savefiledialog1.filename!= "")//detect whether the user entered the save file name
{
Mxmldoc.save (Savefiledialog1.filename)//Mxmldoc save file with private object, Mxmldoc stated earlier
MessageBox.Show ("Save Success");
}
}

/**////<summary>
Convert the loaded XML file to Office files
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button2_Click (object sender, System.EventArgs e)
{
Select the Me node from the private object Dox, and here are some examples of operations on XML objects that refer to MSDN for more information
XmlNode Node=doc. DocumentElement. selectSingleNode ("Me");
XmlElement ele= (XmlElement) node;//gets an XML element
String Pic=ele. GetAttribute ("AA"); get the AA attribute of the Ele element and report it in a temporary string variable pic

Byte[] bytes=convert.frombase64string (pic)//Declaration of a byte[] used to store the data stream of the Base64 decoding conversion.
  
Get file Save Address from Save dialog box
Savefiledialog1.filter = "Office Documents (*.doc, *.xls, *.ppt) |*.doc;*.xls;*.ppt";
Savefiledialog1.title = "Save As 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");
}

}

/**////<summary>
Some initialization behavior when loading a window
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
public void Form1_Load (object sender, System.EventArgs e)
{
MessageBox.Show ("Welcome to Frog Frog Card document Converter");
}
/**////<summary>
Release all temporary variables when the form is unloaded
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
public void Form1_Closed (object sender, System.EventArgs e)
{
Mxmldoc=null;
Doc=null;
}
/**////<summary>
Load an Office file and encode a sequence of flowers as a XmlDocument variable
</summary>
<param name= "Sender" ></param>
<param name= "E" ></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 ("<wawa><me aa=\" {0}\ "/></wawa>", MSTR);
Mxmldoc.loadxml (MSTR);
MessageBox.Show ("loaded successfully");
}

}
/**////<summary>
Loading an XML file into a private object dox
</summary>
<param name= "Sender" ></param>
<param name= "E" ></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 ("loaded successfully");
}

}

}
}

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.