VB.NET序列化在新行序列化之前缺少点

时间:2009-09-17 13:42:01

标签: vb.net serialization

我一直在使用XML序列化,今天我发现了一些非常奇怪的东西。如果我在“点”(。)后面有一个新行,当我反序列化时,我会丢失点。有没有人曾经发生过这种情况?以下是我的序列化代码:

序列化

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

反序列化

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

真正有趣的部分是,如果我在点之后有空格,或删除新的行字符,则没有问题。只有在我发现令人难以置信的点时才会发生这种情况。

以下是我序列化时创建的XML文件的快速示例:

 <?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">
...
  <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>

遇到问题的行是ReceiptAddress节点。

2 个答案:

答案 0 :(得分:0)

MSDN上的这篇文章似乎回答了你的问题。

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

从那篇文章中试试这个:

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

这会创建一个如下所示的根节点:

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

吉姆

答案 1 :(得分:0)

由于我使用的是elses dll,我甚至认为在导入数据时它会修改我的数据。发生的事情是,另一个程序员有一个reg_ex,它在新线之前寻找一个点。这是我的问题,我的悲伤是3个月。