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

发布于 2024-12-19 10:31:22 字数 4031 浏览 1 评论 0 原文

  1. 我使用 xsd.exe 为我正在处理的项目自动生成了一些类。
  2. 我正在使用自动生成的类构建 xml 文档。
  3. 该架构的一个子集非常类似于 html,我试图将原始 HTML 引入我的类中
  4. 为此,我尝试将 HTML 片段反序列化到与我的文档中的 HTML 相对应的类(而不是根节点)中。

理论上听起来不错吧?问题是我的原始文本不会反序列化。我做错了什么吗?我对 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. I auto-generated some classes using xsd.exe for a project I am working on.
  2. I am building an xml document using the auto-generated classes.
  3. A subset of the schema is very html-like and I am trying to bring raw HTML into my classes
  4. To that end, I'm trying to deserialize an HTML snippet into the class (not the root node) that corresponds to HTML in my document.

It sounds good in theory right? The problem is that my raw text wont deserialize. Am I doing something wrong? I have a lot of experience with XML and basic experience with the .NET XmlSerializer. The serializer doesn't error, it just fails to deserialize the contents.

This is the code I am using to deserialize:

    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)

The is the important part of the auto-generated code... It's very large:

'''<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

After execution, the ToValue.itemsField == null. I am expecting an array with one StrucDocTable element.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2024-12-26 10:31:22

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

  1. html 的格式必须完全符合所需的结构。我将 tbody 添加到 HTML 中。
  2. 向标记和 XmlSerializer 构造添加了 XML 命名空间。

    Dim FromValue As String = "
    内部文本
    " Dim ms As New IO.MemoryStream(Encoding.UTF8.GetBytes(FromValue)) 将 ToValue 调暗为 HL7.V3.StrucDocText 暗淡为新 System.Xml.Serialization.XmlSerializer(GetType(HL7.V3.StrucDocText), "urn:hl7-org:v3") ToValue = t.反序列化(毫秒)

之后,我得到的值完全符合预期!我希望这对那里的人有帮助!

Found the answer and I am sharing it for the sanity of all. I had two problems:

  1. The html just be exactly formatted in the desired structure. I added tbody to the HTML.
  2. Added XML namespacing to the markup and the XmlSerializer construction.

    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)
    

After this, I get my values exactly as expected! I hope this helps someone out there!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文