In the c#, use XMLSERIALIZER to sequence the object to xml
This article introduces how to convert the object in C# to the form of XML. If the object has been configured to serialize, this process is very simple. This article will demonstrate how to perform XML serialization on the class, especially in the case of inheritance, and discuss genetic serialization.
Use xmlserializer
To turn the object sequence to XML, you need to use the class. The following code fragment demonstrates how serialization is called
type object:
XmlSerializer
o
This code will create MyObject
XML representation form and assign it to
<code class="language-csharp">XmlSerializer xsSubmit = new XmlSerializer(typeof(MyObject)); MyObject subReq = new MyObject(); string xml = ""; using (var sww = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(sww)) { xsSubmit.Serialize(writer, subReq); xml = sww.ToString(); } }</code>
Facial class serialization subReq
xml
To use this generic class, just call the
method and provide a serialized object:
<code class="language-csharp">public class MySerializer<T> where T : class { public static string Serialize(T obj) { XmlSerializer xsSubmit = new XmlSerializer(typeof(T)); using (var sww = new StringWriter()) { using (XmlTextWriter writer = new XmlTextWriter(sww) { Formatting = Formatting.Indented }) { xsSubmit.Serialize(writer, obj); return sww.ToString(); } } } }</code>
The above is the detailed content of How to Serialize Objects to XML in C# Using XmlSerializer?. For more information, please follow other related articles on the PHP Chinese website!