Monday, 2 March 2015

.net XML deserialization: uppercase and lowecase exception



I'm having some problems to deserialize a XML in .net. This is the error i'm getting:



The opening tag 'A' on line 72 position 56 does not match the end tag of 'a'. Line 72, position 118.


As you can see, is the same tag, but one is uppercase and the other is lower case. My XML has this structure:



<?xml version="1.0"?>
<translationfile xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<translationtext>
<es_text>Spanish text</es_text>
<en_text>English text</en_text>
<developer_comment>Plain text</developer_comment>
</translationtext>
....
</translationfile>


And this is my vb class



Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

'
'Este código fuente fue generado automáticamente por xsd, Versión=2.0.50727.3038.
'

'''<comentarios/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Partial Public Class translationfile

Private itemsField As List(Of translationfileTranslationtext)

'''<comentarios/>
<System.Xml.Serialization.XmlElementAttribute("translationtext", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property Items As List(Of translationfileTranslationtext)
Get
Return Me.itemsField
End Get
Set(value As List(Of translationfileTranslationtext))
Me.itemsField = value
End Set
End Property
End Class

'''<comentarios/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
Partial Public Class translationfileTranslationtext

Private es_textField As String

Private en_textField As String

Private developer_commentField As String

'''<comentarios/>
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property es_text() As String
Get
Return Me.es_textField
End Get
Set(value As String)
Me.es_textField = value
End Set
End Property

'''<comentarios/>
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property en_text() As String
Get
Return Me.en_textField
End Get
Set(value As String)
Me.en_textField = value
End Set
End Property

'''<comentarios/>
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property developer_comment() As String
Get
Return Me.developer_commentField
End Get
Set(value As String)
Me.developer_commentField = value
End Set
End Property
End Class


The problem is that both text could contain HTML code. For some reason i cannot change the text on this tags. For example. This is a real case:



<translationtext>
<es_text><p>Nombre</P></es_text>
<en_text><p>Name</P></en_text>
<developer_comment>irrelevant text</developer_comment>
</translationtext>


When i try to deserialize a XML file, i'm getting the previous error because


is lower case and


is upper case. How can i desarialize it correctly without changing the text?

This is the code i'm using for deserialize:



Dim stream As New IO.StreamReader(path)
Dim ser As New Xml.Serialization.XmlSerializer(GetType(translationfile))
Dim myperfil As New translationfile
myperfil = CType(ser.Deserialize(stream), translationfile) 'This line throws the exception
stream.Close()

No comments:

Post a Comment