Code for converting a file into XML using C #

Source: Internet
Author: User

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 convert the generated xml file to an Office file.
/// Convert the file to xml format, and then use the web service,. NET Remoting, WinSock, and so on for transmission (the latter two can be transmitted without conversion)
/// Xml solves the problem of data transmission in a multi-layer architecture. For example, you can use the Web service on the client to obtain the office file on the server, modify the file, and then send it back to the server.
/// As long as the file is converted into xml format, there are many solutions available, and xml is platform-independent, and you can use it on the server.. net is used to publish web Services, and then the client uses
/// Write an applit Applet in Java to process the sent files. Of course, the example I gave has almost no explicit meaning, but it has given us a lot of inspiration.
/// In addition, if your solution is based on multiple platforms, it is best not to use remote application interface Call (RPC) for interaction between them. Try to use document-based interaction,
/// For example, MSMQ under. net and JMQ of j2ee.
///
/// Many classes have been designed in the example. I have not made too many comments in all the places. For more information, see MSDN. This is my first windows program, something wrong
/// Welcome to your guidance
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{

/** // <Summary>
/// Declare four buttons, one OpenFileDialog, one SaveFileDialog, and two xmldocuments
/// </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 Form Designer support
//
InitializeComponent ();

//
// TODO: add Any constructor code after InitializeComponent calls
//
}

/** // <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

Code generated by Windows Form Designer # code generated by region Windows Form Designer
/** // <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content 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 = "generating xml ";
This. button1.Click + = new System. EventHandler (this. button#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 = "loading 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 = "loading 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. form#load );
This. Closed + = new System. EventHandler (this. Form1_Closed );

}
# Endregion

/** // <Summary>
/// Start the form from this entry
/// </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 button#click (object sender, System. EventArgs e)
{
SaveFileDialog1.Filter = "xml file | *. xml"; // you can specify a filter condition for the files in the displayed dialog box.
SaveFileDialog1.Title = "Save As an xml file"; // you can specify the title of the open dialog box.
SaveFileDialog1.FileName = "";
SaveFileDialog1.ShowDialog (); // open the dialog box

If (saveFileDialog1.FileName! = "") // Check whether the user has entered the file name to save
{
MXmlDoc. Save (saveFileDialog1.FileName); // use the private object mXmlDoc to Save the file. mXmlDoc has previously declared
MessageBox. Show ("saved successfully ");
}
}

/** // <Summary>
/// Convert the loaded xml file to an Office file
/// </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". For detailed instructions on xml Object operations, 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"); // obtain the aa attribute of the ele element and hide it in a temporary string variable pic

Byte [] bytes = Convert. FromBase64String (pic); // declare a byte [] to store Base64 decoded and converted data streams
  
// Obtain the file storage address from the Save dialog box.
SaveFileDialog1.Filter = "Office Documents ents (*. doc, *. xls, *. ppt) | *. doc; *. xls; *. ppt ";
SaveFileDialog1.Title = "Save as an office file ";
SaveFileDialog1.FileName = "";
SaveFileDialog1.ShowDialog ();

If (saveFileDialog1.FileName! = "")
{
// Create a file stream and save it
FileStream outfile = new System. IO. FileStream (saveFileDialog1.FileName, System. IO. FileMode. CreateNew );
Outfile. Write (bytes, 0, (int) bytes. Length );
MessageBox. Show ("saved successfully ");
}

}

/** // <Summary>
/// Some initialization actions during window Loading
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Public void Form1_Load (object sender, System. EventArgs e)
{
MessageBox. Show ("Welcome to frog document converter ");
}
/** // <Summary>
/// Release all temporary variables when uninstalling the form
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Public void formpolicclosed (object sender, System. EventArgs e)
{
MXmlDoc = null;
Doc = null;
}
/** // <Summary>
/// Load the office file and encode the sequence as an 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 ents (*. 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>
/// Load the xml file to the private object
/// </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 ");
}

}

}
}

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.