Serializar un objeto
1. Objeto a xml
Private Function CrearXMLDesdeObj(ByVal objClase As MiClase) As XmlDocument
Dim oXmlDocument As XmlDocument = New XmlDocument()
Dim oXmlSerializer As XmlSerializer = New XmlSerializer(objClase.GetType())
Dim oStringBuilder As StringBuilder = New StringBuilder()
Dim oStringWriter As StringWriterWithEncoding = New StringWriterWithEncoding(oStringBuilder, Encoding.UTF8)
oXmlSerializer.Serialize(oStringWriter, objClase )
oXmlDocument.LoadXml(oStringBuilder.ToString())
Return oXmlDocument
End Function
2. Quitar elemento vacíos
Serializar si no se quieren cosas raras, es muy fácil. El problema es cuando por ejemplo no quieren que salga la etiqueta cuando el valor es null, y por supuesto no se quiere poner la propiedad nulable. Aqui una funcion que borra esos elmentos.
Cuando se pone IsNullable = true si el valor es null se serializa
Public Function EliminarNullElementsFromXML(ByVal oXmlDocument As XmlDocument) As XmlDocument
Dim oXmlDeclaration As XmlDeclaration = CType(oXmlDocument.FirstChild, XmlDeclaration)
Dim oXDocument As XDocument = XDocument.Parse(oXmlDocument.OuterXml)
Dim oXElements As IEnumerable(Of XElement) = oXDocument.Descendants()
Dim oXElementsDelete As List(Of XElement) = New List(Of XElement)
Dim elem_list As IEnumerable(Of XElement) = From elem In oXElements Select elem
For Each element As XElement In elem_list
If (element.IsEmpty) Then
oXElementsDelete.Add(element)
End If
Next
oXElementsDelete.Remove()
oXmlDocument.LoadXml(oXDocument.ToString(SaveOptions.DisableFormatting))
oXmlDocument.InsertBefore(oXmlDeclaration, oXmlDocument.FirstChild)
Return oXmlDocument
End Function
Dim oXmlDeclaration As XmlDeclaration = CType(oXmlDocument.FirstChild, XmlDeclaration)
Dim oXDocument As XDocument = XDocument.Parse(oXmlDocument.OuterXml)
Dim oXElements As IEnumerable(Of XElement) = oXDocument.Descendants()
Dim oXElementsDelete As List(Of XElement) = New List(Of XElement)
Dim elem_list As IEnumerable(Of XElement) = From elem In oXElements Select elem
For Each element As XElement In elem_list
If (element.IsEmpty) Then
oXElementsDelete.Add(element)
End If
Next
oXElementsDelete.Remove()
oXmlDocument.LoadXml(oXDocument.ToString(SaveOptions.DisableFormatting))
oXmlDocument.InsertBefore(oXmlDeclaration, oXmlDocument.FirstChild)
Return oXmlDocument
End Function
3. Serializar Bool que pueden ser null
[Serializable]
public class Foo
{
[XmlIgnore]
public bool? Bar { get; set; }
[XmlAttribute("Bar")]
[EditorBrowsable(EditorBrowsableState.Never)]
public string xmlBar
{
get { return Bar.ToString(); }
set
{
if (string.IsNullOrEmpty(value)) Bar = null;
else Bar = bool.Parse(value);
}
}
}
public class Foo
{
[XmlIgnore]
public bool? Bar { get; set; }
[XmlAttribute("Bar")]
[EditorBrowsable(EditorBrowsableState.Never)]
public string xmlBar
{
get { return Bar.ToString(); }
set
{
if (string.IsNullOrEmpty(value)) Bar = null;
else Bar = bool.Parse(value);
}
}
}
4. Quitar los elementos vacios.
La funcion CleanEmptyTags, quita los elementos vacios
Public Class UtilXml
Public Shared Function Serialize(ByVal objectToSerialize As Object) As String
Dim oXmlDocument As XmlDocument = New XmlDocument()
Dim oXmlSerializer As XmlSerializer = New XmlSerializer(objectToSerialize.GetType())
Dim oStringBuilder As StringBuilder = New StringBuilder()
Dim oStringWriter As StringWriterWithEncoding = New StringWriterWithEncoding(oStringBuilder, Encoding.UTF8)
oXmlSerializer.Serialize(oStringWriter, objectToSerialize)
Return (CleanEmptyTags(oStringWriter.GetStringBuilder().ToString()))
End Function
Private Shared Function CleanEmptyTags(ByVal xml As String) As String
Dim regex As RegularExpressions.Regex = New RegularExpressions.Regex("(\s)*<(\w)*(\s)*/>")
Return regex.Replace(xml, String.Empty)
Return xml
End Function
End Class
Public Shared Function Serialize(ByVal objectToSerialize As Object) As String
Dim oXmlDocument As XmlDocument = New XmlDocument()
Dim oXmlSerializer As XmlSerializer = New XmlSerializer(objectToSerialize.GetType())
Dim oStringBuilder As StringBuilder = New StringBuilder()
Dim oStringWriter As StringWriterWithEncoding = New StringWriterWithEncoding(oStringBuilder, Encoding.UTF8)
oXmlSerializer.Serialize(oStringWriter, objectToSerialize)
Return (CleanEmptyTags(oStringWriter.GetStringBuilder().ToString()))
End Function
Private Shared Function CleanEmptyTags(ByVal xml As String) As String
Dim regex As RegularExpressions.Regex = New RegularExpressions.Regex("(\s)*<(\w)*(\s)*/>")
Return regex.Replace(xml, String.Empty)
Return xml
End Function
End Class