views:

346

answers:

2

Hey guys,

I've been using XML serialization for a while, and today I realized something really odd. If I have a new line right after a "dot" (.), when i deserialize, I lose the dot. Has anyone ever had this happen to them? The following is my serialization code:

Serialize

Dim xmlSerializer As New System.Xml.Serialization.XmlSerializer(GetType(SilverWare.Licensing.Common.StoreLicense), New System.Type() {GetType(SilverWare.Licensing.Common.StationLicense)})

        Dim gen As LicenseGenerator

        If store Is Nothing Then
            Throw New ArgumentNullException("store")
        ElseIf store.StationLicenses Is Nothing Then
            Throw New ArgumentNullException("store.StationLicenses")
        ElseIf store.StationLicenses.Length = 0 Then
            Throw New ArgumentOutOfRangeException("store.StationLicenses", "Must contain at least one element.")
        End If

        ' Create a license generator for issuing new license keys.
        gen = New LicenseGenerator(store)

        ' Generate store key.
        store.LicenseKey = gen.GenerateLicenseKey

        ' Generate individual station keys.
        For Each station In store.StationLicenses
            station.LicenseKey = gen.GenerateLicenseKey(station)
        Next

        ' Write license to file.
        Using xFile As Xml.XmlWriter = Xml.XmlWriter.Create(licenseFile)
            xmlSerializer.Serialize(xFile, store)

            xFile.Close()
        End Using

Deserialize

Dim xmlDeserializer As New System.Xml.Serialization.XmlSerializer(GetType(SilverWare.Licensing.Common.StoreLicense), New System.Type() {GetType(SilverWare.Licensing.Common.StationLicense)})
        Dim result As SilverWare.Licensing.Common.StoreLicense

        Using xFile As Xml.XmlReader = Xml.XmlReader.Create(licenseFile)
            result = DirectCast(xmlDeserializer.Deserialize(xFile), SilverWare.Licensing.Common.StoreLicense)

            xFile.Close()
        End Using

        Return result

The really funny part is that if I have a space after the dot, or remove the new line character, there are no problems. This only happens if it is dot which I find mind boggling.

Here is a quick sample of my XML file that was created when I serialized:

 <?xml version="1.0" encoding="utf-8" ?> 
<StoreLicense xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
...
  <ReceiptAddress>98 N. Washington St.
Berkeley Springs West Virginia</ReceiptAddress> 
  <Name>Ambrae House at Berkeley Springs</Name> 
  <AliasName>Ambrae House</AliasName> 
  <Address1>98 N. Washington St.</Address1> 
  <Address2 /> 
...
</StoreLicense>

The line that is having the problem is the ReceiptAddress Node.

A: 

This post on MSDN seems to answer your question.

MSDN: Serialize String containing only whitespace such as a " " character

From that post, try this:

<XmlAttribute("xml:space")> _
Public SpacePreserve As [String] = "preserve"

This creates a root node like the following:

<DataImportBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:space="preserve">

Jim

Jim
Hey Jim, thanks for the response. Unfortunately this did not solve my issue. I do have data between the node, so it's not white space being removed.
JohnathanKong
A: 

Since I was using someone elses dll, I didn't even think that it would be modifying my data when we imported it. What was happening was that the other programmer had a reg_ex that was looking for a dot before a new line. That was my issue, and my grief for 3 months.

JohnathanKong