Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to deserialize an empty XML element when <x/> is used instead of <x></x>
    primarykey
    data
    text
    <p>I have an XSD schema and XML file which is used to build components at runtime. These objects can have custom settings. In the XSD file, this is defined as:</p> <pre><code>&lt;xs:complexType name="ComponentSettings"&gt; &lt;xs:sequence&gt; &lt;xs:any minOccurs="0"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; </code></pre> <p>and is instantiated in the XSD as:</p> <pre><code>&lt;xs:complexType name="Component"&gt; &lt;xs:sequence&gt; &lt;xs:element name="Version" type="VersionRecord"/&gt; &lt;xs:element name="Settings" type="ComponentSettings"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="Name" type="xs:string" use="required"/&gt; &lt;/xs:complexType&gt; </code></pre> <p>I encounter an interesting problem when deserializing the XML file. If I have a component with no settings, and the XML looks like this:</p> <pre><code>&lt;Settings&gt;&lt;/Settings&gt; </code></pre> <p>I have no issues.</p> <p>If, however, the XML looks like this:</p> <pre><code>&lt;Settings/&gt; </code></pre> <p>Then deserialization will fail silently following this tag and I end up with an incomplete record.</p> <p>I am using the following code generated by xsd2code:</p> <pre><code>&lt;System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18060"), _ System.SerializableAttribute(), _ System.ComponentModel.DesignerCategoryAttribute("code"), _ System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=True)&gt; _ Partial Public Class Component Implements System.ComponentModel.INotifyPropertyChanged Private versionField As VersionRecord Private settingsField As System.Xml.XmlElement Private nameField As String Private Shared sSerializer As System.Xml.Serialization.XmlSerializer '''&lt;summary&gt; '''Component class constructor '''&lt;/summary&gt; Public Sub New() MyBase.New() Me.versionField = New VersionRecord() End Sub &lt;System.Xml.Serialization.XmlElementAttribute(Order:=0)&gt; _ Public Property Version() As VersionRecord Get Return Me.versionField End Get Set(value As VersionRecord) If (Not (Me.versionField) Is Nothing) Then If (versionField.Equals(value) &lt;&gt; True) Then Me.versionField = value Me.OnPropertyChanged("Version") End If Else Me.versionField = value Me.OnPropertyChanged("Version") End If End Set End Property &lt;System.Xml.Serialization.XmlElementAttribute(Order:=2)&gt; _ Public Property Settings() As System.Xml.XmlElement Get Return Me.settingsField End Get Set(value As System.Xml.XmlElement) If (Not (Me.settingsField) Is Nothing) Then If (settingsField.Equals(value) &lt;&gt; True) Then Me.settingsField = value Me.OnPropertyChanged("Settings") End If Else Me.settingsField = value Me.OnPropertyChanged("Settings") End If End Set End Property &lt;System.Xml.Serialization.XmlAttributeAttribute()&gt; _ Public Property Name() As String Get Return Me.nameField End Get Set(value As String) If (Not (Me.nameField) Is Nothing) Then If (nameField.Equals(value) &lt;&gt; True) Then Me.nameField = value Me.OnPropertyChanged("Name") End If Else Me.nameField = value Me.OnPropertyChanged("Name") End If End Set End Property #Region "Serialize/Deserialize" '''&lt;summary&gt; '''Serializes current Component object into an XML document '''&lt;/summary&gt; '''&lt;returns&gt;string XML value&lt;/returns&gt; Public Overridable Overloads Function Serialize(ByVal encoding As System.Text.Encoding) As String Dim streamReader As System.IO.StreamReader = Nothing Dim memoryStream As System.IO.MemoryStream = Nothing Try memoryStream = New System.IO.MemoryStream() Dim xmlWriterSettings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings() xmlWriterSettings.Encoding = encoding Dim xmlWriter As System.Xml.XmlWriter = xmlWriter.Create(memoryStream, xmlWriterSettings) Serializer.Serialize(xmlWriter, Me) memoryStream.Seek(0, System.IO.SeekOrigin.Begin) streamReader = New System.IO.StreamReader(memoryStream) Return streamReader.ReadToEnd Finally If (Not (streamReader) Is Nothing) Then streamReader.Dispose() End If If (Not (memoryStream) Is Nothing) Then memoryStream.Dispose() End If End Try End Function Public Overridable Overloads Function Serialize() As String Return Serialize(Encoding.UTF8) End Function '''&lt;summary&gt; '''Deserializes workflow markup into an Component object '''&lt;/summary&gt; '''&lt;param name="xml"&gt;string workflow markup to deserialize&lt;/param&gt; '''&lt;param name="obj"&gt;Output Component object&lt;/param&gt; '''&lt;param name="exception"&gt;output Exception value if deserialize failed&lt;/param&gt; '''&lt;returns&gt;true if this XmlSerializer can deserialize the object; otherwise, false&lt;/returns&gt; Public Overloads Shared Function Deserialize(ByVal xml As String, ByRef obj As Component, ByRef exception As System.Exception) As Boolean exception = Nothing obj = CType(Nothing, Component) Try obj = Deserialize(xml) Return True Catch ex As System.Exception exception = ex Return False End Try End Function Public Overloads Shared Function Deserialize(ByVal xml As String, ByRef obj As Component) As Boolean Dim exception As System.Exception = Nothing Return Deserialize(xml, obj, exception) End Function Public Overloads Shared Function Deserialize(ByVal xml As String) As Component Dim stringReader As System.IO.StringReader = Nothing Try stringReader = New System.IO.StringReader(xml) Return CType(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)), Component) Finally If (Not (stringReader) Is Nothing) Then stringReader.Dispose() End If End Try End Function '''&lt;summary&gt; '''Serializes current Component object into file '''&lt;/summary&gt; '''&lt;param name="fileName"&gt;full path of outupt xml file&lt;/param&gt; '''&lt;param name="exception"&gt;output Exception value if failed&lt;/param&gt; '''&lt;returns&gt;true if can serialize and save into file; otherwise, false&lt;/returns&gt; Public Overridable Overloads Function SaveToFile(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef exception As System.Exception) As Boolean exception = Nothing Try SaveToFile(fileName, encoding) Return True Catch e As System.Exception exception = e Return False End Try End Function Public Overridable Overloads Function SaveToFile(ByVal fileName As String, ByRef exception As System.Exception) As Boolean Return SaveToFile(fileName, Encoding.UTF8, exception) End Function Public Overridable Overloads Sub SaveToFile(ByVal fileName As String) SaveToFile(fileName, Encoding.UTF8) End Sub Public Overridable Overloads Sub SaveToFile(ByVal fileName As String, ByVal encoding As System.Text.Encoding) Dim streamWriter As System.IO.StreamWriter = Nothing Try Dim xmlString As String = Serialize(encoding) streamWriter = New System.IO.StreamWriter(fileName, False, encoding.UTF8) streamWriter.WriteLine(xmlString) streamWriter.Close() Finally If (Not (streamWriter) Is Nothing) Then streamWriter.Dispose() End If End Try End Sub '''&lt;summary&gt; '''Deserializes xml markup from file into an Component object '''&lt;/summary&gt; '''&lt;param name="fileName"&gt;string xml file to load and deserialize&lt;/param&gt; '''&lt;param name="obj"&gt;Output Component object&lt;/param&gt; '''&lt;param name="exception"&gt;output Exception value if deserialize failed&lt;/param&gt; '''&lt;returns&gt;true if this XmlSerializer can deserialize the object; otherwise, false&lt;/returns&gt; Public Overloads Shared Function LoadFromFile(ByVal fileName As String, ByVal encoding As System.Text.Encoding, ByRef obj As Component, ByRef exception As System.Exception) As Boolean exception = Nothing obj = CType(Nothing, Component) Try obj = LoadFromFile(fileName, encoding) Return True Catch ex As System.Exception exception = ex Return False End Try End Function Public Overloads Shared Function LoadFromFile(ByVal fileName As String, ByRef obj As Component, ByRef exception As System.Exception) As Boolean Return LoadFromFile(fileName, Encoding.UTF8, obj, exception) End Function Public Overloads Shared Function LoadFromFile(ByVal fileName As String, ByRef obj As Component) As Boolean Dim exception As System.Exception = Nothing Return LoadFromFile(fileName, obj, exception) End Function Public Overloads Shared Function LoadFromFile(ByVal fileName As String) As Component Return LoadFromFile(fileName, Encoding.UTF8) End Function Public Overloads Shared Function LoadFromFile(ByVal fileName As String, ByVal encoding As System.Text.Encoding) As Component Dim file As System.IO.FileStream = Nothing Dim sr As System.IO.StreamReader = Nothing Try file = New System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read) sr = New System.IO.StreamReader(file, encoding) Dim xmlString As String = sr.ReadToEnd sr.Close() file.Close() Return Deserialize(xmlString) Finally If (Not (file) Is Nothing) Then file.Dispose() End If If (Not (sr) Is Nothing) Then sr.Dispose() End If End Try End Function #End Region #Region "Clone method" '''&lt;summary&gt; '''Create a clone of this Component object '''&lt;/summary&gt; Public Overridable Function Clone() As Component Return CType(Me.MemberwiseClone, Component) End Function #End Region End Class </code></pre> <p>I am curious about why this is happening as well as if there is a way to not experience this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload