0

这是我第一次使用 XML 序列化,我不确定是否可以提交嵌套重复表,或者如何在数组中序列化数组。

我有一个 Infopath 表单,其中包含“资源”重复表中的“周”重复表。这是 XML 输出:

<my:AllocateResource>
    <my:Resource>
        <my:Person>
            <my:DisplayName>User 1</my:DisplayName>
            <my:AccountId>49808</my:AccountId>
            <my:AccountType>User</my:AccountType>
        </my:Person>
    </my:Resource>
    <my:Weeks>
        <my:WeekNumber>24</my:WeekNumber>
        <my:Hours>20</my:Hours>
    </my:Weeks>
    <my:Weeks>
        <my:WeekNumber>28</my:WeekNumber>
        <my:Hours>15</my:Hours>
        </my:Weeks>
    <my:RequestID>1</my:RequestID>
    <my:StartDate>2013-08-01</my:StartDate>
    <my:EndDate>2013-08-14</my:EndDate>
</my:AllocateResource>
<my:AllocateResource>
    <my:Resource>
        <my:Person>
            <my:DisplayName>User2</my:DisplayName>
            <my:AccountId>49841</my:AccountId>
            <my:AccountType>User</my:AccountType>
        </my:Person>
    </my:Resource>
    <my:Weeks>
        <my:WeekNumber>25</my:WeekNumber>
        <my:Hours>10</my:Hours>
    </my:Weeks>
    <my:RequestID>2</my:RequestID>
    <my:StartDate>2013-08-01</my:StartDate>
    <my:EndDate>2013-08-14</my:EndDate>
</my:AllocateResource>

我正在尝试将其序列化到我的 ASMX Web 服务中以写入我的 SQL 数据库。我可以让它只使用一个重复表,但是当我尝试在其中放置第二个重复表时,我没有通过任何数据。这是我的带有序列化的 RepeatingTable 类:

<System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-08-12T19:02:25", IsNullable:=False)> _Public Class AllocateResources
<System.Xml.Serialization.XmlArray("AllocateResource")> _
Public Resource As Resource()
Public RequestID As Integer
Public StartDate As Date
Public EndDate As Date

End Class
    Public Class Resource
    Public Person As Person()
    Public Weeks As Weeks()
End Class

Public Class Weeks
    Public WeekNumber As Integer
    Public Hours As Decimal
End Class

Public Class Person
    Public DisplayName As String
    Public AccountId As String
    Public AccountType As String
End Class

我的 Web 服务 SOAP Evelope 看起来像这样,注意 Person 和 Weeks 节点不包括子节点:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SubmitRepeatingTable xmlns="http://tempuri.org/">
      <myRepTable xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2013-08-12T19:02:25">
        <AllocateResource>
          <Resource>
            <Person xsi:nil="true" />
            <Weeks xsi:nil="true" />
          </Resource>
          <Resource>
            <Person xsi:nil="true" />
            <Weeks xsi:nil="true" />
          </Resource>
        </AllocateResource>
        <RequestID>int</RequestID>
        <StartDate>dateTime</StartDate>
        <EndDate>dateTime</EndDate>
      </myRepTable>
    </SubmitRepeatingTable>
  </soap:Body>
</soap:Envelope>

这是我的网络服务代码:

 Public Sub SubmitRepeatingTable(myRepTable As RepeatingTable)

        Dim myConnection As SqlConnection = New SqlConnection()
        myConnection = New SqlConnection(connectionString)
        myConnection.Open()
        Dim Command As New SqlClient.SqlCommand("usp_add_allocation")
        Command.CommandType = CommandType.StoredProcedure
        Command.Connection = myConnection

        For i As Integer = 0 To myRepTable.Resource.Length - 1
            Dim RequestID As Integer = myRepTable.RequestID
            Dim AccountID As String = myRepTable.Resource(i).Person(i).AccountId
            Dim StartDate As String = myRepTable.StartDate
            Dim EndDate As String = myRepTable.EndDate
            For j As Integer = 0 To myRepTable.Resource(i).Weeks.Length - 1
                Dim WeekNumber As Integer = myRepTable.Resource(i).Weeks(j).WeekNumber
                Dim Hours As Decimal = myRepTable.Resource(i).Weeks(j).Hours
4

1 回答 1

0

好的,我想出了一个简单的方法来解决这个问题:

Visual Studio 2012 有一个 XSD 架构到序列化编码工具。

在 Infopath 中,将表单另存为源(从文件菜单)。

然后转到开始 --> Visual Studio --> Visual Studio 工具 --> 开发人员命令提示符。导航到您保存 infpath 源的目录并键入:

xsd myschema.xsd /classes

我还添加了

/language:VB

之后得到VB代码。它将以正确的格式将代码保存到同一文件夹中。比自己写要容易得多。

于 2013-08-21T18:57:00.127 回答