Pergunta

This is my first time using XML serialization and I'm not sure if I can submit nested repeating tables, or how to serialize arrays within arrays.

I have an Infopath form with a "Weeks" repeating table within a "Resource" repeating table. This is the XML output:

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

I am trying to serialize this into my ASMX Web Service for writing to my SQL database. I can get it working with only one repeating table, but when I try and place a second one inside I'm not getting any data through. This is my RepeatingTable Class with the serialization:

<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

My Web service SOAP Evelope looks like this, Notice the Person and Weeks nodes don't inclide the child nodes:

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

This is my WebService code:

 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
Foi útil?

Solução

OK, I figured out a simple way to solve this:

Visual Studio 2012 has an XSD Schema to Serialization coding tool.

In Infopath, save your form as source (from the file menu).

Then go to Start --> Visual Studio --> Visual Studio Tools --> Developer Command Prompt. Navigate to the directory you saved your infpath source and type:

xsd myschema.xsd /classes

I also added

/language:VB

afterwards to get VB code. It will save the code to the same folder in the correct format. So much easier than writing it myself.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top