Xml.Serialization

System.Xml.Serialization
This was added in .NET 1.1
This namespace represents a collection of attribute objects that control how the XmlSerializer serializes and deserializes an object.


XmlSerializer

System.Xml.Serialization.XmlSerializer


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.
System.XML.Serialization


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.



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

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