Today I wrote a tool to convert object classes to XML, and convert XML to corresponding object classes.
The Code is as follows:
XML Conversion class
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Data;
Using System. IO;
Using System. Xml. Serialization;
Using System. Xml;
Namespace WFXML
{
Public class XmlUtil
{
# Region deserialization
/// <Summary>
/// Deserialization
/// </Summary>
/// <Param name = "type"> type </param>
/// <Param name = "xml"> XML string </param>
/// <Returns> </returns>
Public static object Deserialize (Type type, string xml)
{
Try
{
Using (StringReader sr = new StringReader (xml ))
{
XmlSerializer xmldes = new XmlSerializer (type );
Return xmldes. Deserialize (sr );
}
}
Catch (Exception e)
{
Return null;
}
}
/// <Summary>
/// Deserialization
/// </Summary>
/// <Param name = "type"> </param>
/// <Param name = "xml"> </param>
/// <Returns> </returns>
Public static object Deserialize (Type type, Stream stream)
{
XmlSerializer xmldes = new XmlSerializer (type );
Return xmldes. Deserialize (stream );
}
# Endregion
# Region serialize XML files
/// <Summary>
/// Serialize the XML file
/// </Summary>
/// <Param name = "type"> type </param>
/// <Param name = "obj"> Object </param>
/// <Returns> </returns>
Public static string Serializer (Type type, object obj)
{
MemoryStream Stream = new MemoryStream ();
// Create a serialized object
XmlSerializer xml = new XmlSerializer (type );
Try
{
// Serialize the object
Xml. Serialize (Stream, obj );
}
Catch (InvalidOperationException)
{
Throw;
}
Stream. Position = 0;
StreamReader sr = new StreamReader (Stream );
String str = sr. ReadToEnd ();
Return str;
}
# Endregion
# Region converts XML to DATATABLE
/// <Summary>
/// Convert XML to DATATABLE
/// </Summary>
/// <Param name = "FileURL"> </param>
/// <Returns> </returns>
Public static DataTable XmlAnalysisArray ()
{
Try
{
String FileURL = System. Configuration. ConfigurationManager. receivettings ["Client"]. ToString ();
DataSet ds = new DataSet ();
Ds. ReadXml (FileURL );
Return ds. Tables [0];
}
Catch (Exception ex)
{
System. Web. HttpContext. Current. Response. Write (ex. Message. ToString ());
Return null;
}
}
/// <Summary>
/// Convert XML to DATATABLE
/// </Summary>
/// <Param name = "FileURL"> </param>
/// <Returns> </returns>
Public static DataTable XmlAnalysisArray (string FileURL)
{
Try
{
DataSet ds = new DataSet ();
Ds. ReadXml (FileURL );
Return ds. Tables [0];
}
Catch (Exception ex)
{
System. Web. HttpContext. Current. Response. Write (ex. Message. ToString ());
Return null;
}
}
# Endregion
# Region get the value of the corresponding XML Node
/// <Summary>
/// Summary: Get the value of the corresponding XML Node
/// </Summary>
/// <Param name = "stringRoot"> mark an XML node </param>
/// <Returns> return the value of the corresponding XML node. </returns>
Public static string XmlAnalysis (string stringRoot, string xml)
{
If (stringRoot. Equals ("") = false)
{
Try
{
XmlDocument XmlLoad = new XmlDocument ();
XmlLoad. LoadXml (xml );
Return XmlLoad. DocumentElement. SelectSingleNode (stringRoot). InnerXml. Trim ();
}
Catch (Exception ex)
{
}
}
Return "";
}
# Endregion
}
}
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Namespace WFXML
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
// Convert an XML file to a corresponding object class
String xml = @ "<Depart>
<DepartID> 123 </DepartID>
<PerSons>
<PerSon>
<Name> Zhang San </name>
<Age> 3 </age>
</PerSon>
<PerSon>
<Name> LITH </name>
<Age> 56 </age>
</PerSon>
</PerSons>
</Depart>
";
Var Info = (Depart) XmlUtil. Deserialize (typeof (Depart), xml );
MessageBox. Show (Info. DepartID );
}
Private void button2_Click (object sender, EventArgs e)
{
// Convert an object class to XML
Depart dp = new Depart ();
Dp. DepartID = "qq ";
Dp. DepartID = "123 ";
PerSon p1 = new PerSon ();
P1.name = "zhang ";
P1.age = "4 ";
Dp. PerSons [0] = p1;
String resutl = XmlUtil. Serializer (typeof (Depart), dp );
MessageBox. Show (resutl );
}
}
Public class Depart
{
Public string DepartID;
Public PerSon [] PerSons = new PerSon [2];
}
Public class PerSon
{
Public string name;
Public string age;
}
}
From cheese Column