2011-12-03 61 views
0
  1. 我自動生成了一些使用xsd.exe的類,用於我正在處理的項目。
  2. 我正在使用自動生成的類建立一個xml文檔。
  3. 模式的一個子集非常類似於html,我試圖將原始HTML帶入我的類
  4. 爲此,我試圖將一個HTML片段反序列化到類(不是根節點)中對應於我的文檔中的HTML。

這聽起來不錯,理論上正確嗎?問題是我的原始文本不會反序列化。難道我做錯了什麼?我有很多使用XML和.NET XmlSerializer的基本經驗。序列化程序不會出錯,它只是無法反序列化內容。.NET XmlSerializer不反序列化XmlElementAttribute對象數組

這是我使用的反序列化的代碼:

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元素的數組。

任何幫助,將不勝感激。

+0

請發佈XML示例。 –

回答

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) 
    

在此之後,我得到我的價值觀完全按預期!我希望這可以幫助那裏的人!

相關問題