How to: Begin Sample with Serialization and Deserialization an Object
2012-06-27 21:59
453 查看
To serialize an object,
first create the object that is to be serialized
second set its public properties and fields.
third you must determine the transport format in which the XML stream is to be stored, either as a stream or as a file.
For example, if the XML stream must be saved in a permanent form, create a FileStream object.
the steps detail in code:
Create the object and set its public fields and properties.
Construct a XmlSerializer using the type of the object. For more information, see the XmlSerializer class constructors.
Call the Serialize method to generate either an XML stream or a file representation of the object's public properties and fields. The following example creates a file.
first create the object that is to be serialized
second set its public properties and fields.
third you must determine the transport format in which the XML stream is to be stored, either as a stream or as a file.
For example, if the XML stream must be saved in a permanent form, create a FileStream object.
the steps detail in code:
Create the object and set its public fields and properties.
Construct a XmlSerializer using the type of the object. For more information, see the XmlSerializer class constructors.
Call the Serialize method to generate either an XML stream or a file representation of the object's public properties and fields. The following example creates a file.
public class MySerializableClass() { public string name{get;set;} public string id(get;set;)//public properties private string _sex; public int age; //public fields }
serialization code: MySerializableClass myObject = new MySerializableClass(); // Insert code to set properties and fields of the object. XmlSerializer mySerializer = new XmlSerializer(typeof(MySerializableClass)); // To write to a file, create a StreamWriter object. StreamWriter myWriter = new StreamWriter("myFileName.xml"); mySerializer.Serialize(myWriter, myObject); myWriter.Close();
<?xml version="1.0" encoding="utf-8"?> <MySerializableClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <age>12</age> <name>wq</name> <id>1</id> </MySerializableClass>
deserialization code:
MySerializableClass myObject1; // Construct an instance of the XmlSerializer with the type // of object that is being deserialized. XmlSerializer mySerializer1 = new XmlSerializer(typeof(MySerializableClass)); // To read the file, create a FileStream. FileStream myFileStream1 = new FileStream("myFileName.xml", FileMode.Open); // Call the Deserialize method and cast to the object type. myObject1 = (MySerializableClass)mySerializer1.Deserialize(myFileStream1);
相关文章推荐
- Deep copy with serialization and deserialization (C#)
- Map Serialization and Deserialization with Jackson
- coreseek 中文分词搜索引擎 too many words (84) for quorum; replacing with an AND
- JHTP自测题_第十五章_文件、流和对象序列化(Files, Streams, and Object Serialization)
- Difference between event.target and event.currentTarget properties in an event object.
- Binary Serialization and BinaryFormatter with WebServices
- Object-Oriented Analysis and Design with Applications
- Working with user roles and permissions in SharePoint Object Model
- org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {解决方法
- If you object has an "Id' property, it will be set with the generated Id from MongoDB.
- AFNetworking 关于JSON text did not start with array or object and option to allow fragments not set 错误
- Get translation and rotation matrix of an object
- Java-Java I/O流解读之Object Serialization and Object Streams
- XML Serialization and Deserialization
- The object or library file 'xxx.obj' was created with an older compiler than other obj
- 笔记: SAP How-to Guide: How To... Create an Application and Security profile in SMP 3.0
- 论文笔记 | Exploit All the Layers: Fast and Accurate CNN Object Detector with SDP and CRC
- 对于执行Get-SPWeb遇到错误“Cannot find an SPWeb object with Id or Url”的解决方案
- Creating an Audio Calling App with Ionic and PhoneRTC
- G3: serialization and deserialization