.NET XmlSerializer不反序列化XmlElementAttribute对象数组

时间:2011-12-03 01:04:12

标签: xml xml-serialization

  1. 我使用xsd.exe为我正在处理的项目自动生成了一些类。
  2. 我正在使用自动生成的类构建xml文档。
  3. 架构的一个子集非常像html,我正在尝试将原始HTML引入我的类
  4. 为此,我正在尝试将HTML片段反序列化到与文档中的HTML对应的类(而不是根节点)中。
  5. 理论上听起来不错吗?问题是我的原始文本不会反序列化。难道我做错了什么?我在XML和.NET XmlSerializer的基本经验方面有很多经验。序列化程序没有错误,它只是无法反序列化内容。

    这是我用来反序列化的代码:

        Dim FromValue As String = "<StrucDoc.Text><table><tr><td>Inside Text</td></tr></table></StrucDoc.Text>"
        Dim ms As New IO.MemoryStream(Encoding.UTF8.GetBytes(FromValue))
        Dim ToValue As HL7.V3.StrucDocText
        Dim t As New System.Xml.Serialization.XmlSerializer(GetType(HL7.V3.StrucDocText))
        ToValue = t.Deserialize(ms)
    

    这是自动生成代码的重要部分......它非常大:

    '''<remarks/>
    <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
     System.SerializableAttribute(), _
     System.Diagnostics.DebuggerStepThroughAttribute(), _
     System.ComponentModel.DesignerCategoryAttribute("code"), _
     System.Xml.Serialization.XmlTypeAttribute(TypeName:="StrucDoc.Text", [Namespace]:="urn:hl7-org:v3")> _
    Partial Public Class StrucDocText
    
    Private itemsField() As Object
    
    Private textField() As String
    
    Private idField As String
    
    Private languageField As String
    
    Private styleCodeField As String
    
    Private mediaTypeField As String
    
    Public Sub New()
        MyBase.New()
        Me.mediaTypeField = "text/x-hl7-text+xml"
    End Sub
    
    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("br", GetType(StrucDocBr)), _
     System.Xml.Serialization.XmlElementAttribute("content", GetType(StrucDocContent)), _
     System.Xml.Serialization.XmlElementAttribute("footnote", GetType(StrucDocFootnote)), _
     System.Xml.Serialization.XmlElementAttribute("footnoteRef", GetType(StrucDocFootnoteRef)), _
     System.Xml.Serialization.XmlElementAttribute("linkHtml", GetType(StrucDocLinkHtml)), _
     System.Xml.Serialization.XmlElementAttribute("list", GetType(StrucDocList)), _
     System.Xml.Serialization.XmlElementAttribute("paragraph", GetType(StrucDocParagraph)), _
     System.Xml.Serialization.XmlElementAttribute("renderMultiMedia", GetType(StrucDocRenderMultiMedia)), _
     System.Xml.Serialization.XmlElementAttribute("sub", GetType(StrucDocSub)), _
     System.Xml.Serialization.XmlElementAttribute("sup", GetType(StrucDocSup)), _
     System.Xml.Serialization.XmlElementAttribute("table", GetType(StrucDocTable))> _
    Public Property Items() As Object()
        Get
            Return Me.itemsField
        End Get
        Set(ByVal value As Object())
            Me.itemsField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlTextAttribute()> _
    Public Property Text() As String()
        Get
            Return Me.textField
        End Get
        Set(ByVal value As String())
            Me.textField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute(DataType:="ID")> _
    Public Property ID() As String
        Get
            Return Me.idField
        End Get
        Set(ByVal value As String)
            Me.idField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute(DataType:="NMTOKEN")> _
    Public Property language() As String
        Get
            Return Me.languageField
        End Get
        Set(ByVal value As String)
            Me.languageField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute(DataType:="NMTOKENS")> _
    Public Property styleCode() As String
        Get
            Return Me.styleCodeField
        End Get
        Set(ByVal value As String)
            Me.styleCodeField = value
        End Set
    End Property
    
    '''<remarks/>
    <System.Xml.Serialization.XmlAttributeAttribute()> _
    Public Property mediaType() As String
        Get
            Return Me.mediaTypeField
        End Get
        Set(ByVal value As String)
            Me.mediaTypeField = value
        End Set
    End Property
    End Class
    

    执行后,ToValue.itemsField == null。我期待一个包含StrucDocTable元素的数组。

    任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

找到答案,我为了所有人的理智而分享它。我有两个问题:

  1. html只是在所需的结构中精确格式化。我在HTML中添加了tbody。
  2. 在标记和XmlSerializer构造中添加了XML命名空间。

    Dim FromValue As String = "<StrucDoc.Text xmlns=""urn:hl7-org:v3""><table><tbody><tr><td>Inside Text</td></tr></tbody></table></StrucDoc.Text>"
    Dim ms As New IO.MemoryStream(Encoding.UTF8.GetBytes(FromValue))
    Dim ToValue As HL7.V3.StrucDocText
    Dim t As New System.Xml.Serialization.XmlSerializer(GetType(HL7.V3.StrucDocText), "urn:hl7-org:v3")
    ToValue = t.Deserialize(ms)
    
  3. 在此之后,我完全按照预期获得我的价值观!我希望这可以帮助那里的人!