De-Serialization Problem

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="line-height:normal; margin:0cm 0cm 10pt <span style="font-family:Verdana,sans-serif; color:black; font-size:8pt I am trying to deserialize an xml file. All data deserializes fine except for one element in the xml document. The element
that fails to deserialize is <Last_x0020_trade>. In my class, I map this element to a String property, which is always empty regardless of the data in the xml. If I change the element name in the xml file to <Lastx0020trade>, and modify
my class accordingly, the data de-serializes. I thought it might be something to do with the underscores so I also tried changing the element name to <Last_1234_trade> - this de-serialised ok.
<p style="line-height:normal; margin:0cm 0cm 10pt <span style="font-family:Verdana,sans-serif; color:black; font-size:8pt In an attempt to debug the problem I tried to implement a custom de-serialization by implementing ISerializable but code execution
never dropped into either
<p style="line-height:normal; margin:0cm 0cm 10pt <span style="font-family:Verdana,sans-serif; color:black; font-size:8pt New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
<p style="line-height:normal; margin:0cm 0cm 10pt <span style="font-family:Verdana,sans-serif; color:black; font-size:8pt or
<p style="line-height:normal; margin:0cm 0cm 10pt <span style="font-family:Verdana,sans-serif; color:black; font-size:8pt Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData.
<p style="line-height:normal; margin:0cm 0cm 10pt <span style="font-family:Verdana,sans-serif; color:black; font-size:8pt Any help on how I might overcome this problem would be appreciated
<div style="background-color:white; color:black
<pre><span style="color:green ================Test Page Code=====================

<span style="color:blue Imports System.IO

<span style="color:blue Imports System.Xml.Serialization

<span style="color:blue Imports System.Xml

<span style="color:blue Public <span style="color:blue Class _Default

<span style="color:blue Inherits System.Web.UI.Page

<span style="color:blue Private m_objDownload <span style="color:blue As clsDownload

<span style="color:blue Protected <span style="color:blue Sub Page_Load(<span style="color:blue ByVal sender <span style="color:blue As <span style="color:blue Object, <span style="color:blue ByVal e <span style="color:blue As System.EventArgs) <span style="color:blue Handles <span style="color:blue Me.Load

<span style="color:blue If <span style="color:blue Not IsPostBack <span style="color:blue Then

RefreshList()

<span style="color:blue End <span style="color:blue If

<span style="color:blue End <span style="color:blue Sub

<span style="color:blue Function GetDownload() <span style="color:blue As clsDownload

<span style="color:blue Dim objReturn <span style="color:blue As clsDownload = <span style="color:blue Nothing

 

<span style="color:green Connect to wcf service

<span style="color:blue Dim objSharePriceDownload <span style="color:blue As <span style="color:blue New SharePriceServiceRef.Service1Client(<span style="color:#a31515 "BasicHttpBinding_IService1")

<span style="color:green Create xmlDocument from the Node at

<span style="color:blue Dim xmlDoc <span style="color:blue As <span style="color:blue New XmlDocument

xmlDoc.LoadXml(objSharePriceDownload.GetAllData4DeskTopXml().OuterXml)

<span style="color:green Create memory Stream from downloaded xmlDoc

<span style="color:blue Dim stream <span style="color:blue As MemoryStream = <span style="color:blue New MemoryStream()

xmlDoc.Save(stream)

stream.Seek(0, SeekOrigin.Begin)

objReturn = clsDownload.GetNew(<span style="color:blue New StreamReader(stream))

<span style="color:blue Return objReturn

<span style="color:blue End <span style="color:blue Function

 

<span style="color:blue Function GetDataFromFile() <span style="color:blue As clsDownload

<span style="color:blue Dim objReturn <span style="color:blue As clsDownload = <span style="color:blue Nothing

 

<span style="color:green Connect to wcf service

<span style="color:green Dim objSharePriceDownload As New SharePriceServiceRef.Service1Client("BasicHttpBinding_IService1")

<span style="color:green Create xmlDocument from the Node at

<span style="color:blue Dim xmlDoc <span style="color:blue As <span style="color:blue New XmlDocument

xmlDoc.Load(<span style="color:#a31515 "C:UsersTerryTestList.xml")

 

<span style="color:green Create memory Stream from downloaded xmlDoc

<span style="color:blue Dim stream <span style="color:blue As MemoryStream = <span style="color:blue New MemoryStream()

xmlDoc.Save(stream)

<span style="color:green Set stream pointer back to beginning of stream before

<span style="color:green de-serialization

stream.Seek(0, SeekOrigin.Begin)

 

objReturn = clsDownload.GetNew(<span style="color:blue New StreamReader(stream))

<span style="color:blue Return objReturn

<span style="color:blue End <span style="color:blue Function

 

<span style="color:blue Sub RefreshList()

<span style="color:blue Try

m_objDownload = GetDataFromFile() <span style="color:green GetDownload()

GridView1.AutoGenerateColumns = <span style="color:blue True

GridView1.DataSource = m_objDownload.CompanyPrices

GridView1.DataBind()

<span style="color:blue Catch ex <span style="color:blue As Exception

Response.Write(ex.Message)

<span style="color:blue End <span style="color:blue Try

<span style="color:blue End <span style="color:blue Sub

<span style="color:blue Protected <span style="color:blue Sub Button1_Click(<span style="color:blue ByVal sender <span style="color:blue As <span style="color:blue Object, <span style="color:blue ByVal e <span style="color:blue As EventArgs) <span style="color:blue Handles Button1.Click

RefreshList()

<span style="color:blue End <span style="color:blue Sub

<span style="color:blue End <span style="color:blue Class

<span style="color:green ================End of Page Code=====================

<span style="color:green ================Class Code===========================

<span style="color:blue Imports System.Xml.Serialization

<span style="color:blue Imports System.IO

<XmlRoot(ElementName:=<span style="color:#a31515 "SharePrices")> _

<span style="color:blue Public <span style="color:blue Class clsDownload

<span style="color:blue Public <span style="color:blue Shared <span style="color:blue Function GetNew(<span style="color:blue ByVal sr <span style="color:blue As StreamReader) <span style="color:blue As clsDownload

<span style="color:blue Dim objDownload <span style="color:blue As <span style="color:blue New clsDownload

<span style="color:blue Dim ser <span style="color:blue As <span style="color:blue New XmlSerializer(objDownload.<span style="color:blue GetType)

objDownload = ser.Deserialize(sr)

<span style="color:blue Return objDownload

<span style="color:blue End <span style="color:blue Function

<XmlElement(<span style="color:#a31515 "CompPrices")> _

<span style="color:blue Public <span style="color:blue Property CompanyPrices() <span style="color:blue As List(Of clsCompanyPrices)

<XmlElement(<span style="color:#a31515 "Indices")> _

<span style="color:blue Public <span style="color:blue Property Indices() <span style="color:blue As List(Of clsIndex)

 

<span style="color:blue End <span style="color:blue Class

<span style="color:blue Imports System.Xml.Serialization

<span style="color:blue Imports System.IO

 

<XmlRoot(ElementName:=<span style="color:#a31515 "CompPrice")> _

<span style="color:blue Public <span style="color:blue Class clsCompanyPrices

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "Symbol")> _

<span style="color:blue Public <span style="color:blue Property Symbol <span style="color:blue As <span style="color:blue String

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "Company")> _

<span style="color:blue Public <span style="color:blue Property Company <span style="color:blue As <span style="color:blue String

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "Price")> _

<span style="color:blue Public <span style="color:blue Property Price <span style="color:blue As <span style="color:blue Decimal

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "Currency")> _

<span style="color:blue Public <span style="color:blue Property Currency <span style="color:blue As <span style="color:blue String

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "Last_x0020_trade")> _

<span style="color:blue Public <span style="color:blue Property LastTrade <span style="color:blue As <span style="color:blue String

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "indexid")> _

<span style="color:blue Public <span style="color:blue Property IndexID <span style="color:blue As <span style="color:blue Integer

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "change")> _

<span style="color:blue Public <span style="color:blue Property Change <span style="color:blue As <span style="color:blue Decimal

<span style="color:blue End <span style="color:blue Class

<span style="color:blue Imports System.Xml.Serialization

<span style="color:blue Imports System.IO

<XmlRoot(ElementName:=<span style="color:#a31515 "Indices")> _

<span style="color:blue Public <span style="color:blue Class clsIndex

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "id")> _

<span style="color:blue Public <span style="color:blue Property ID <span style="color:blue As <span style="color:blue Integer

<XmlElementAttribute(ElementName:=<span style="color:#a31515 "name")> _

<span style="color:blue Public <span style="color:blue Property Name <span style="color:blue As <span style="color:blue String

<span style="color:blue End <span style="color:blue Class

 

<XmlRoot(ElementName:=<span style="color:#a31515 "SharePrices")> _

<span style="color:blue Public <span style="color:blue Class clsIndexList

<span style="color:blue Public <span style="color:blue Shared <span style="color:blue Function GetNew(<span style="color:blue ByVal sr <span style="color:blue As StreamReader) <span style="color:blue As clsIndexList

<span style="color:blue Dim objIndexList <span style="color:blue As <span style="color:blue New clsIndexList

<span style="color:blue Dim ser <span style="color:blue As <span style="color:blue New XmlSerializer(objIndexList.<span style="color:blue GetType)

objIndexList = ser.Deserialize(sr)

<span style="color:blue Return objIndexList

<span style="color:blue End <span style="color:blue Function

<XmlElement(<span style="color:#a31515 "Indices")> _

<span style="color:blue Public <span style="color:blue Property Items() <span style="color:blue As List(Of clsIndex)

<span style="color:blue End <span style="color:blue Class

<span style="color:green ==================End of Classes======================

<pre lang=<span style="color:#a31515 "x-xml"><span style="color:green ==================Sample XML File C:UsersTerryTestList.xml==================

<SharePrices xmlns=<span style="color:#a31515 "">

<Indices>

<id>1</id>

<name>FTSE100</name>

</Indices>

<Indices>

<id>2</id>

<name>DOW JONES</name>

</Indices>

<CompPrices>

<Symbol>ATUK.L</Symbol>

<Company>(A)UK</Company>

<Price>4.35</Price>

<Currency>GBP</Currency>

<Last_x0020_trade>2010-11-08T13:35:00+00:00</Last_x0020_trade>

<indexid>4</indexid>

<change>0.22</change>

</CompPrices>

<CompPrices>

<Symbol>OPM.L</Symbol>

<Company>1PM</Company>

<Price>0.05</Price>

<Currency>GBP</Currency>

<Last_x0020_trade>2010-11-08T13:26:00+00:00</Last_x0020_trade>

<indexid>4</indexid>

<change>0.99</change>

</CompPrices>

</SharePrices>



[/code]


View the full article
 
Back
Top