如何在没有包含元素的情况下序列化“Any”元素序列

时间:2013-08-05 14:45:41

标签: c# serialization xsd

我需要将一些数据序列化,以作为消息通过Web服务传递,该服务将根据模式验证数据。我已经构建了一个对象模型,但我在一个小区域内遗漏了一些东西。

这是架构:

<xs:element name="UserDetailsMessage" minOccurs="0">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="URDUSERDETAILS" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:any processContents="skip" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

这是我的班级:

[Serializable()] public class UserDetailsMessage
{
    private USERDETAILS myUserDetails;

    [XmlElementAttribute("USERDETAILS")]
    public USERDETAILS USERDETAILS {
        get {
            if (myUserDetails == null)
                myUserDetails = new USERDETAILS();
            return this.myUserDetails;
        }
        set { this.myUserDetails = value; }
    }

    public bool URDUSERDETAILSSpecified {
        get {
            if (myUserDetails == null)
                return false;
            return true;
        }
    }

}

[Serializable()] public class USERDETAILS
{
    private List<System.Xml.XmlElement> anyField;

    [XmlAnyElementAttribute(), XmlElementAttribute("Any")]
    public List<System.Xml.XmlElement> Any {
        get { return this.anyField; }
        set { this.anyField = value; }
    }

}

我在XmlElementAttribute元素上使用Any以使其序列化为没有容器的元素序列,但XML输出不会出现,因为我认为它会:

<UserDetailsMessage>
    <USERDETAILS>
        <Any>
            <TEST_DATA_HERE />
        </Any>
    </USERDETAILS>
</UserDetailsMessage>

这导致XmlDocument.Validate方法输出此验证错误:The 'Any' element is not declared

据我所知,我想要创建的输出应该如下所示:

<UserDetailsMessage>
    <USERDETAILS>
        <TEST_DATA_HERE />
    </USERDETAILS>
</UserDetailsMessage>

XmlElementAttribute以某种方式与XmlAnyElementAttribute进行交互以产生此输出还是我在某处犯了明显的错误?或者,如何在没有包含元素的情况下序列化“Any”元素序列?

1 个答案:

答案 0 :(得分:3)

我终于有时间坐下来创建只考虑特定领域的测试项目,并达到以下目的。

<Serializable()> Public Class NotificationMessage

    Private myUserData As UserData

    Public Sub New()
        myUserData = New UserData()
    End Sub

    <System.Xml.Serialization.XmlElementAttribute("UserData")> _
    Public Property UserData() As UserData
        Get
            Return Me.myUserData
        End Get
        Set(value As UserData)
            Me.myUserData = value
        End Set
    End Property

End Class

<Serializable()> Public Class UserData

    Private anyField As List(Of System.Xml.XmlElement)

    Public Sub New()
        Me.anyField = New List(Of System.Xml.XmlElement)()
    End Sub

    <XmlAnyElementAttribute()>
    Public Property Any() As List(Of System.Xml.XmlElement)
        Get
            Return Me.anyField
        End Get
        Set(value As List(Of System.Xml.XmlElement))
            Me.anyField = value
        End Set
    End Property
End Class

如果组合XmlElementXmlAnyElement并将其应用于UserData类的Any属性,则这是输出:

<NotificationMessage>
  <UserData>
    <Any>
      <TEST_DATA_HERE />
    </Any>
  </UserData>
</NotificationMessage>

如果仅应用XmlElement,则为输出:

<NotificationMessage>
  <UserData>
    <Any>
      <TEST_DATA_HERE />
    </Any>
  </UserData>
</NotificationMessage>

这是仅应用了XmlAnyElement的输出:

<NotificationMessage>
  <UserData>
    <TEST_DATA_HERE />
  </UserData>
</NotificationMessage>

因此我们可以看到,为了序列化一系列'Any'元素,我们必须单独使用 XmlAnyElement属性。