开发者

WCF serialization of base64Binary not being encoded correctly

Greetings!

So I have a base class that defines a base64Binary property to be returned as an embedded file in XML. I am not getting any errors, however the string being returned is not being encoded as base64. Any ideas? I have included the base class, and also the code where it is being called. Any help would be greatly appreciated!!

'--------------------------------------------------
'BinaryObjectType type
'--------------------------------------------------
<XmlType(TypeName:="BinaryObjectType",Namespace:=Declarations.SchemaVersion),Serializable, _
EditorBrowsable(EditorBrowsableState.Advanced)> _
Public Class BinaryObjectType
    '*********************** format attribute ***********************
    <XmlAttribute(AttributeName:="format", Form:=XmlSchemaForm.Unqualified, DataType:="string", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __format As String

    <XmlIgnore()> _
    Public Property format As String
        Get
            format = __format
        End Get
        Set(ByVal Value As String)
            __format = Value
        End Set
    End Property

    '*********************** mimeCode attribute ***********************
    <XmlAttribute(AttributeName:="mimeCode", Form:=XmlSchemaForm.Unqualified, Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __mimeCode As String

    <XmlIgnore()> _
    Public Property mimeCode As String
        Get
            mimeCode = _开发者_如何学C_mimeCode
        End Get
        Set(ByVal Value As String)
            __mimeCode = Value
        End Set
    End Property

    '*********************** encodingCode attribute ***********************
    <XmlAttribute(AttributeName:="encodingCode", Form:=XmlSchemaForm.Unqualified, DataType:="normalizedString", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __encodingCode As String

    <XmlIgnore()> _
    Public Property encodingCode As String
        Get
            encodingCode = __encodingCode
        End Get
        Set(ByVal Value As String)
            __encodingCode = Value
        End Set
    End Property

    '*********************** characterSetCode attribute ***********************
    <XmlAttribute(AttributeName:="characterSetCode", Form:=XmlSchemaForm.Unqualified, DataType:="normalizedString", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __characterSetCode As String

    <XmlIgnore()> _
    Public Property characterSetCode As String
        Get
            characterSetCode = __characterSetCode
        End Get
        Set(ByVal Value As String)
            __characterSetCode = Value
        End Set
    End Property

    '*********************** uri attribute ***********************
    <XmlAttribute(AttributeName:="uri", Form:=XmlSchemaForm.Unqualified, DataType:="anyURI", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __uri As String

    <XmlIgnore()> _
    Public Property uri As String
        Get
            uri = __uri
        End Get
        Set(ByVal Value As String)
            __uri = Value
        End Set
    End Property

    '*********************** filename attribute ***********************
    <XmlAttribute(AttributeName:="filename", Form:=XmlSchemaForm.Unqualified, DataType:="string", Namespace:=Declarations.SchemaVersion), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __filename As String

    <XmlIgnore()> _
    Public Property filename As String
        Get
            filename = __filename
        End Get
        Set(ByVal Value As String)
            __filename = Value
        End Set
    End Property

    '*********************** XmlText field ***********************
    <XmlText(DataType:="base64Binary"), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public __Value As Byte()

    <XmlIgnore()> _
    Public Property Value() As Byte()
        Get
            Value = __Value
        End Get
        Set(ByVal val As Byte())
            __Value = val
        End Set
    End Property

    '*********************** Constructor ***********************
    Public Sub New()
    End Sub
End Class

Here is the snippet where it is being built:

Dim binFile As New BinaryObjectType

Select Case Trim(UCase(objFromDB.FileFormat))
    Case "PDF"
        binFile.mimeCode = "application/pdf"
    Case "DOC"
        binFile.mimeCode = "application/ms-word"
    Case "DOCX"
        binFile.mimeCode = "application/ms-word"
    Case "GIF"
        binFile.mimeCode = "image/gif"
    Case "JPG"
        binFile.mimeCode = "image/jpeg"
    Case "JPEG"
        binFile.mimeCode = "image/jpeg"
    Case "PNG"
        binFile.mimeCode = "image/png"
    Case Else
        binFile.mimeCode = "text/html"
    End Select

    binFile.Value = objFile
    attachmentArea.EmbeddedData = binFile
    attachmentAreaList.Add(attachmentArea)
    candidateProfile.AttachmentCollection = attachmentAreaList        
    candidateProfileList.Add(candidateProfile)        
    candidateArea.CandidateProfileCollection = candidateProfileList        
    candidateAreaList.Add(candidateArea)        
    dataArea.Show = showArea        
    dataArea.CandidateCollection = candidateAreaList        
    showResponse.DataArea = dataArea

    Return showResponse

I am not getting errors, and I am getting a result for the binFile.Value, but it isn't being base64 encoded.

Any ideas? Thanks in advance!!!


How do you see that the string is not base64-encoded? I tried a similar scenario (see below), and the Byte() was properly encoded in base-64.

Public Class StackOverflow_6186859_751090

    Public Class Declarations
        Public Const SchemaVersion As String = "http://my.namespace"
    End Class

    'BinaryObjectType type 
    '-------------------------------------------------- 
    <XmlType(TypeName:="BinaryObjectType", Namespace:=Declarations.SchemaVersion), Serializable(), _
     EditorBrowsable(EditorBrowsableState.Advanced)> _
     Public Class BinaryObjectType
        <XmlAttribute(AttributeName:="format", Form:=XmlSchemaForm.Unqualified, DataType:="string", Namespace:=Declarations.SchemaVersion), _
             EditorBrowsable(EditorBrowsableState.Advanced)> _
             Public __format As String
        <XmlIgnore()> _
        Public Property format() As String
            Get
                format = __format
            End Get
            Set(ByVal Value As String)
                __format = Value
            End Set
        End Property

        <XmlAttribute(AttributeName:="mimeCode", Form:=XmlSchemaForm.Unqualified, Namespace:=Declarations.SchemaVersion), _
         EditorBrowsable(EditorBrowsableState.Advanced)> _
        Public __mimeCode As String
        <XmlIgnore()> _
        Public Property mimeCode() As String
            Get
                mimeCode = __mimeCode
            End Get
            Set(ByVal Value As String)
                __mimeCode = Value
            End Set
        End Property

        <XmlText(DataType:="base64Binary"), _
         EditorBrowsable(EditorBrowsableState.Advanced)> _
        Public __Value As Byte()
        <XmlIgnore()> _
        Public Property Value() As Byte()
            Get
                Value = __Value
            End Get
            Set(ByVal val As Byte())
                __Value = val
            End Set
        End Property

    End Class

    Public Shared Sub Test()
        Dim ms As MemoryStream = New MemoryStream()
        Dim ser As XmlSerializer = New XmlSerializer(GetType(BinaryObjectType))
        Dim obj As BinaryObjectType = New BinaryObjectType()
        obj.format = "PNG"
        obj.mimeCode = "image/png"
        obj.Value = Encoding.UTF8.GetBytes("Some random text which will be encoded as bytes")
        ser.Serialize(ms, obj)
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()))
    End Sub
End Class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜