使用Web Service将Infopath重复表数据提交到SQL

时间:2013-08-15 15:15:46

标签: vb.net web-services xml-serialization infopath

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

我有一个Infopath表格,其中包含" Weeks"重复表内的资源"重复的表。这是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>

这是我的WebService代码:

 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

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决这个问题的简单方法:

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

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

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

xsd myschema.xsd /classes

我还添加了

/language:VB

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