System.Xml.Serialization.XmlSerializer

Aded in .NET 1.1
Can be used to serialise .NET types to XML files.
link - learn.microsoft.com/en-us/dotnet/standard/serialization/examples-of-xml-serialization


Data is commonly sent and retrieved in an XML format but processing the XML directly is a nightmare
To make this transition easier you can convert from XML to an object.
Converting between XML and custom business objects is known as serialization and there is a namespace dedicated to this.
Was used by ASMX web services


Classes - System.XML.Serialization.XMLSerializer
Lets you convert objects to XML and visa versa.
This class will also automatically create arrays when there are nested elements


converts object to a stream of bytes
to persist the state of an object to a storage medium so an exact copy can be recreated at a later stage
to send an object by value from one app domain to another
can be used to save objects to the clipboard


.NET provides an automate serilization mechanism by using Reflection
When an object is serialized, the name of the class, the assembly and all the data members are written to storage.


Serialize and Deserialize can work with a Stream, XmlWriter/XmlReader or TextWriter/TextReader



Serializing a Class

<System.Serializable()> 
Public Class MyClass

Private MyField As Boolean
Private m_bMyFlag As Boolean

Public Sub New
   m_bMyFlag = False
End Sub

Public Property MyFlag() As Boolean
   Get
      Return m_bFlag
   End Get

   Set(ByVal value As Boolean)
      m_bMyFlag = value
   End Set
End Property

Public Class AnotherClass
End Class

End Class


Renaming Classes

[XMLType("newclass")] 
Public class MyClass
{
}

Renaming Elements

<System.XML.XMLElement(ElementName="newelement")>_ 
[XMLElement("newelement")]
public string myelement
{
}

Renaming Attributes

[XMLAttribute("newattribute")] 
public boolean myattribute
{
}

<newclass newattribute="one"> 
   <newelement>"two"</newelement>
</newclass>

Ignoring Properties

[XMLIgnore] 
Public string myproperty

Including

[XMLInclude] 



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext