This article mainly describes the serialization and deserialization of XML and objects. And a few simple serialization and deserialization methods are attached for everyone to use.
Let's say we have two of these classes in a Web project
Copy CodeThe code is as follows:
public class Member
{
public string Num {get; set;}
public string Name {get; set;}
}
public class Team
{
public string Name;
Public List Members {get; set;}
}
Suppose we need to post an instance of the team class to a URL,
Of course, this function can be done by hiding the domain submission using form.
What if the team includes 30 data?
To differentiate each member, we have to add a suffix to the name of the parameter. This requires a large list of hidden fields to complete:
Copy CodeThe code is as follows:
@model Team
Can you imagine if the team were more complicated and nested more?
Well, even if you're willing to pass the data, it's a headache for the other person to see a parameter name.
We all know that objects can not be transmitted directly in the network, but there is a remedy.
XML (extensible Markup Language)Extensible Markup language itself is designed to store data, and any object can be described in XML. Take the team class as an example:
Copy CodeThe code is as follows:
Development
001
Marry
002
John
Such an XML document represents an instance of the team.
Smart crossing should have thought that XML is a vector that can be used as object information in the network, because it is in textual form.
How do you convert an XML document to an object?
The XmlSerializer class is doing the job.
namespaces: System.Xml.Serialization
Assembly: System.Xml (in System.Xml.dll)
A Encodehelper class that provides a serialization and deserialization method is now shown here.
The Deserialize method converts an XML string to an object of the specified type;
The Serialize method converts the object to an XML string.
Copy CodeThe code is as follows:
///
Provides serialization deserialization of XML documents
///
public sealed class Encodehelper
{
///
Deserializes the XML string to the specified type
///
public static object Deserialize (string Xml, Type thistype)
{
XmlSerializer XmlSerializer = new XmlSerializer (thistype);
object result;
Try
{
using (StringReader StringReader = new StringReader (XML))
{
result = Xmlserializer.deserialize (StringReader);
}
}
catch (Exception innerexception)
{
BOOL flag = FALSE;
if (Xml! = null)
{
if (Xml.startswith (Encoding.UTF8.GetString (Encoding.UTF8.GetPreamble ())))
{
Flag = true;
}
}
throw new ApplicationException (string. Format ("couldn ' t parse XML: ' {0} '; Contains BOM: {1}; Type: {2}. ",
XML, Flag, Thistype.fullname), innerexception);
}
return result;
}
///
Serialize object object to XML string
///
public static string Serialize (object objecttoserialize)
{
string result = NULL;
Try
{
XmlSerializer XmlSerializer = new XmlSerializer (Objecttoserialize.gettype ());
using (MemoryStream MemoryStream = new MemoryStream ())
{
XmlTextWriter XmlTextWriter = new XmlTextWriter (MemoryStream, New UTF8Encoding (false));
xmltextwriter.formatting = formatting.indented;
Xmlserializer.serialize (XmlTextWriter, objecttoserialize);
Xmltextwriter.flush ();
Xmltextwriter.close ();
UTF8Encoding utf8encoding = new UTF8Encoding (false, True);
result= utf8encoding.getstring (Memorystream.toarray ());
}
}
catch (Exception innerexception)
{
throw new ApplicationException ("couldn ' t Serialize Object:" + objecttoserialize.gettype (). Name, innerexception);
}
return result;
}
}
To use this class, you need to add the following references
Using System;
Using System.Text;
Using System.IO;
Using System.Xml;
Using System.Xml.Serialization;
Let's use a console program to illustrate how this class works. Here is the main function of the program.
Copy CodeThe code is as follows:
static void Main (string[] args)
{
List Members = new List ();
Member member1 = new Member {Name = "Marry", Num = "001"};
Member member2 = new Member {Name = "John", Num = "002"};
Members.add (Member1);
Members.add (MEMBER2);
Team Team = new Team {Name = "development", Members = members};
var xml =encodehelper.serialize (team);//serialization
Console.Write (XML);//print serialized XML string
Console.ReadLine ();
Team Newteam = encodehelper.deserialize (XML, typeof (Team)) as team;//requires an explicit type conversion when deserializing
Console.WriteLine ("Team Name:" +newteam.name);//Displays the Newteam object after deserialization
foreach (Var member in Newteam.members)
{
Console.WriteLine ("Member Num:" + Member. Num);
Console.WriteLine ("Member Name:" + Member. Name);
}
Console.ReadLine ();
}
After executing the Console.Write (XML) line, you can see the printed XML document.
Copy CodeThe code is as follows:
Development
001
Marry
002
John
The example I gave at the beginning of the article is identical.
The final deserialization of the Newteam object prints out the result.
Team name:development
Member num:001
Member Name:marry
Member num:002
Member Name:john
Back to our opening example of Web communication,
Using XML serialization and deserialization for object passing, we just need to serialize the object to be passed as an XML string, and use a hidden domain for form submission.
The receiver then deserializes the received XML string into a preset object. The premise is that both parties must agree that serialization is consistent with the process of deserialization and that the object is the same.
Finally, let's take a look at some of the features that govern the process of serializing and deserializing operations. Let's change the starting class:
Copy CodeThe code is as follows:
public class Member
{
[XmlElement ("Member_num")]
public string Num {get; set;}
public string Name {get; set;}
}
[XmlRoot ("Our_team")]
public class Team
{
[Xmlignore]public string Name;
Public List Members {get; set;}
}
Then we execute the console program again, and the serialization result becomes this:
Copy CodeThe code is as follows:
001
Marry
002
John
The original root node team becomes the Our_team,member child node Num becomes the Member_num, and the team's name child node is ignored.
The visible attribute XmlRoot can control the display and operation of the root node, while the XmlElement is for child nodes. If some members are marked XmlIgnore, they are ignored during serialization and deserialization.
The specifics of these features can be viewed on MSDN, not much.
With this knowledge, the transmission of object data in the network should be difficult to see officer. ^_^
http://www.bkjia.com/PHPjc/327539.html www.bkjia.com true http://www.bkjia.com/PHPjc/327539.html techarticle This article mainly describes the serialization and deserialization of XML and objects. And a few simple serialization and deserialization methods are attached for everyone to use. Suppose we have ... in a Web project .