Adding Rows with an element where maxoccurs > 0

lankylad

Member
Joined
Jan 12, 2006
Messages
7
Programming Experience
10+
The following code works fine

Dim
WorkRow As DataRow = dsOut.Tables("SearchRequest").NewRow()

WorkRow["Surname"] = m_forename
WorkRow("PostTown") = m_posttown
dsOut.Tables("SearchRequest").Rows.Add(WorkRow)

but I now wish to add

WorkRow["Forename"] = m_forename

where Forename is defined in the XSD as

<xsd:element name="Forename" minOccurs="0" maxOccurs="2" />

How do I add these two entries?
 
Xsd says there can be 0(?) or 1 or 2 Forename elements in Xml file, so you have to make room in table for 2 forename columns else concenate any number of forenames into 1 table column
 
What is the format of the code? I have tried for example

WorkRow("Forename")(0) = m_forename
WorkRow("Forename")(1) = m_forename1

but I cant find any combination trhat works
 
I might have missed something with the new click&generate features.. but how was dataset dsOut and table SearchRequest created ?
Since you're asking, you probably didn't create the table yourself (and the columns in it). When displaying the table, it should be easy to spot all the columns.
Can you do the following: ?
WorkRow("Forename") = m_forename & " " & m_forename1
 
The dataset was created by reading in the XSD as in:

dsout.readxmlschema("schemaname")

I'm trying to create some XML that looks like this:

<Body>
<Surname>Smith</Surname>
<Forename>Alan</Forename>
<Forename>Ian</Forename>
<PostTown>Ian</PostTown>
</Body>
 
Ok, I have investigated some :)
I created a xml schema that should be silimar to yours:
VB.NET:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="[URL="http://www.w3.org/2001/XMLSchema"]http://www.w3.org/2001/XMLSchema[/URL]" elementFormDefault="qualified">
<xs:element name="persons">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="person" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="surname" type="xs:string"/>
            <xs:element name="forename" type="xs:string"
            minOccurs="0" maxOccurs="2"/>
            <xs:element name="posttown" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>
Then I added this code to have a look the table(s) and columns:
VB.NET:
Dim dsout As New DataSet
Dim sb As New System.Text.StringBuilder
dsout.ReadXmlSchema("test.xsd")
For Each t As DataTable In dsout.Tables
  sb.Append("table " & t.TableName & vbNewLine)
  For Each c As DataColumn In t.Columns
    sb.Append("column " & c.ColumnName & vbNewLine)
  Next
  sb.Append(vbNewLine)
Next
For Each rel As DataRelation In dsout.Relations
  sb.Append("relation " & rel.ToString & vbNewLine)
Next
MsgBox(sb.ToString)
This is the output from that string:
VB.NET:
table person
column surname
column posttown
column person_Id
 
table forename
column forename_Column
column person_Id
 
relation person_forename

So, you see there is automatically added an ID column to each person, and created another table for the forename that can be several items (+ID), plus added the relation between the two tables in dataset.

Hope this helps!
 
Back
Top