In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what are common classes and properties of XML serialization and deserialization in .NET", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what are common classes and properties of XML serialization and deserialization in .NET".
What are serialization and deserialization?
Serialization (seriallization): converts objects to data formats that are easy to transfer. Common serialization formats: binary format, byte array, json string, xml string.
Deseriallization: the process of restoring serialized data to an object.
XmlSerializer class
This class provides serialized services in a highly loosely coupled way. Your classes do not need to inherit special base classes, and they do not need to implement special interfaces. Instead, you just need to add custom features to your classes or their public domains and read / write properties. XmlSerializer reads these features through the reflection mechanism and uses them to map your classes and class members to xml elements and attributes (serialization and deserialization between objects and XML documents).
.net-XML serialization and deserialization simple sample code using System.IO;using System.Xml.Serialization Namespace Practices.Common {public class SimpleSerializer {/ Model entity object serialized to XML string / object type / Model entity object / public static string SerializeXMLL (T t) {using (StringWriter sw = new StringWriter ()) { XmlSerializer xmlSerializer = new XmlSerializer (t.GetType ()) XmlSerializer.Serialize (sw, t); return sw.ToString () }} / XML is deserialized to object / xml string / public static T Deserialize (string xml) {XmlSerializer xmlSerializer = new XmlSerializer (typeof (T)); StringReader stringReader = new StringReader (xml) Return (T) xmlSerializer.Deserialize (stringReader);}} XmlTypeAttribute class
This class mainly controls the XML nodes that are generated when the attribute target is serialized by XML.
Application example / Envelope/// [XmlType (TypeName = "envelope")] public class CityRes {public Header header {get; set;} public Response response {get; set;} XmlElementAttribute class
This class is used to instruct public fields or properties to represent XML elements when XML serializes or deserializes the object that contains them.
Application example / Envelope/// public class CityRes {/ header / [XmlElement ("header")] public Header header {get; set;} / response / [XmlElement ("response")] public Response response {get; set;}} XmlAttributeAttribute class
This class specifies that XML serialization must serialize class members as XML attributes.
Application example / Version/// public class Version {/ port / [XmlAttribute ("port")] public string port {get; set;} / host / [XmlAttribute ("host")] public string host {get; set } / text / / [XmlAttribute ("text")] public string text {get; set;}} XmlArrayAttribute class
This class is mainly used for the application of XML element arrays (which is equivalent to the declaration of collections).
Application example [XmlArray] public Item [] Items {get {return items;} set {items = value;}} XmlTextAttribute class
Members decorated with this feature are treated as XML text when Xml documents are serialized or deserialized.
Application example [System.Xml.Serialization.XmlTextAttribute ()] public string [] Text {get {return this.textField;} set {this.textField = value;}} XmlIgnoreAttribute class
Indicates that the object decorated by this property will not serialize the element specified by the property when Xml serializes.
Apply the sample public class TeamGroup {[XmlIgnore] public string Comment; public string GroupName;} to control the attribute summary of XML serialization
By applying the features in the following table to classes and class members, you can control how XmlSerializer serializes or deserializes instances of that class. To understand how these properties control XML serialization, see using properties to control XML serialization.
Properties apply to objects that specify XmlAnyAttributeAttribute public fields, properties, parameters, or return values of an array of XmlAttribute objects. When deserialized, the array is populated with XmlAttribute objects that represent all XML properties unknown to the schema. XmlAnyElementAttribute public fields, properties, parameters, or return values of an array of XmlElement objects. When deserialized, the array is populated with XmlElement objects that represent all XML elements unknown to the schema. The return value of a XmlArrayAttribute public field, property, parameter, or array that returns a complex object. Array members are generated as members of the XML array. The return value of a XmlArrayItemAttribute public field, property, parameter, or array that returns a complex object. You can insert a derived type of an array. It is usually used with XmlArrayAttribute. XmlAttributeAttribute public fields, properties, parameters, or return values. Members are serialized as XML attributes. XmlChoiceIdentifierAttribute public fields, properties, parameters, or return values. You can use enumerations to further disambiguate members. XmlElementAttribute public fields, properties, parameters, or return values. The field or attribute is serialized as a XML element. XmlEnumAttribute as a public field of enumerated identifiers. The element name of the enumeration member. XmlIgnoreAttribute public properties and public fields. When serializing a class, you should ignore properties or fields. The XmlIncludeAttribute public derived class declaration and the return value of the public method of the Web Services description language (WSDL) document. This class should be included when generating a schema to be identified at serialization time. XmlRootAttribute public class declaration. Controls XML serialization that is considered an attribute target of the XML root element. Use this attribute to further specify the namespace and element name. XmlTextAttribute public properties and public fields. Properties or fields should be serialized as XML text. XmlTypeAttribute public class declaration. Name and namespace of the XML type. The above is about the content of this article on "what are the common classes and properties of XML serialization and deserialization in .NET?" I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.