Imports System.Collections.Generic
Imports System.Collections
Imports System.Collections.ObjectModel
Imports System.Reflection
Imports System.Xml.Serialization
Imports System.Xml.Schema
Public Class CategoryInfo
Public Sub New(SearchTerm as String)
Me.SearchTerm = SearchTerm
Public Property SearchTerm As String
Public Class OptionsSerializable
Public Property Unsupported As List(Of XmlElement)
<System.Xml.Serialization.XmlElement(ElementName:="ListeCategoriesExt")>
Public ReadOnly Property Categories As List(Of CategoryInfo) = New List(Of CategoryInfo)
Shared Sub AddCategory(Categories as IList(Of CategoryInfo), Name as String)
Categories.Add(New CategoryInfo(Name))
Public ReadOnly Property ListeCategories As EnumerableBridge(Of String, CategoryInfo) = New EnumerableBridge(Of String, CategoryInfo)(_categories, AddressOf AddCategory, Function(c) c.SearchTerm)
Public Property ServeurExchangeUrl As String
Public Property VersionExchange As String
Public Class EnumerableBridge(Of TSource, TTarget)
Implements IEnumerable(Of TSource)
Private _refCollection As IList(Of TTarget)
Private _add As Action(Of IList(Of TTarget), TSource)
Private _converterFrom As Func(Of TTarget, TSource)
Throw New NotImplementedException()
Public Sub New(refCollection As IList(Of TTarget), add As Action(Of IList(Of TTarget), TSource), converterFrom As Func(Of TTarget, TSource))
_refCollection = refCollection
_converterFrom = converterFrom
Public Sub Add(item As TSource)
_add(_refCollection, item)
Public Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Public Function GetEnumerator() As IEnumerator(Of TSource) Implements IEnumerable(Of TSource).GetEnumerator
Return _refCollection.Select(_converterFrom).GetEnumerator()
Dim Root as OptionsSerializable = New OptionsSerializable() With { _
.ServeurExchangeUrl = "hello" _
Root.Categories.Add(New CategoryInfo("item 1"))
Dim root2 = xml.LoadFromXML(Of OptionsSerializable)()
Dim xml2 = root2.GetXml()
Assert.IsTrue(Root.ListeCategories.Count = Root2.ListeCategories.Count)
Assert.IsTrue(xml = xml2)
Public Module XmlSerializationHelper
<System.Runtime.CompilerServices.Extension> _
Public Function LoadFromXML(Of T)(xmlString As String) As T
Using reader As New StringReader(xmlString)
Return DirectCast(New XmlSerializer(GetType(T)).Deserialize(reader), T)
<System.Runtime.CompilerServices.Extension> _
Public Function GetXml(Of T)(obj As T, Optional omitStandardNamespaces As Boolean = False) As String
Dim ns As XmlSerializerNamespaces = Nothing
If omitStandardNamespaces Then
ns = New XmlSerializerNamespaces()
<System.Runtime.CompilerServices.Extension> _
Public Function GetXml(Of T)(obj As T, ns as XmlSerializerNamespaces) As String
Using textWriter = New StringWriter()
Dim settings = New XmlWriterSettings() With { _
Using xmlWriter = System.Xml.XmlWriter.Create(textWriter, settings)
Dim serializer as New XmlSerializer(obj.GetType())
serializer.Serialize(xmlWriter, obj, ns)
Return textWriter.ToString()
<System.Runtime.CompilerServices.Extension> _
Public Function GetOuterXml(node as XmlNode, Optional indent as Boolean = true) as String
Using textWriter = New StringWriter()
Dim settings = New XmlWriterSettings() With { _
.OmitXmlDeclaration = true _
Using xmlWriter = System.Xml.XmlWriter.Create(textWriter, settings)
Return textWriter.ToString()