Imports System.Data Imports System.Xml Module Module1 Sub Main() ' Create a DataSet to hold our test values Dim dsTest As New DataSet() dsTest.ReadXmlSchema(New XmlTextReader("..\Test.xsd")) ' Load the tables with test data With dsTest.Tables("TableOne").Rows .Add(New Object(1) {1, "Apple"}) .Add(New Object(1) {2, "Orange"}) .Add(New Object(1) {3, "Tomato"}) End With With dsTest.Tables("TableTwo").Rows .Add(New Object(1) {1, "Fruit"}) .Add(New Object(1) {2, "Vegetable"}) End With With dsTest.Tables("TableOne_TableTwo_Relationship").Rows .Add(New Object(2) {1, 1, 1}) .Add(New Object(2) {2, 2, 1}) .Add(New Object(2) {3, 3, 1}) .Add(New Object(2) {4, 3, 2}) End With ' Since the XML Schema (Test.xsd) already has the Relationships defined, ' we are ready to retrieve data from the DataSet Dim ItemRow As DataRow Dim XRefRow As DataRow Dim TypeRow As DataRow Console.WriteLine("Displaying all rows from Table One:") Console.WriteLine() Console.WriteLine("TableOneID | TableOneDesc") Console.WriteLine("-------------------------") For Each ItemRow In dsTest.Tables("TableOne").Rows Console.WriteLine("{0,10} {1}", ItemRow("TableOneID"), ItemRow("TableOneDesc")) Next Console.WriteLine() Console.WriteLine("Displaying all rows from Table Two:") Console.WriteLine() Console.WriteLine("TableTwoID | TableTwoDesc") Console.WriteLine("-------------------------") For Each ItemRow In dsTest.Tables("TableTwo").Rows Console.WriteLine("{0,10} {1}", ItemRow("TableTwoID"), ItemRow("TableTwoDesc")) Next Console.WriteLine() Console.WriteLine("Displaying all rows from Table One and the related rows from Table Two:") Console.WriteLine() Console.WriteLine("TableOneID | TableOneDesc + TableTwoID | TableTwoDesc") Console.WriteLine("-------------------------------------------------------") For Each ItemRow In dsTest.Tables("TableOne").Rows For Each XRefRow In ItemRow.GetChildRows("TableOneTableOne_TableTwo_Relationship") TypeRow = dsTest.Tables("TableTwo").Rows.Find(XRefRow("TableTwoID")) Console.WriteLine("{0,10} {1,-12} + {2,10} {3,-12}", _ ItemRow("TableOneID"), ItemRow("TableOneDesc"), _ TypeRow("TableTwoID"), TypeRow("TableTwoDesc")) Next Next Console.Write("Press ENTER to close") Console.Read() End Sub End ModuleSample XML:
<?xml version="1.0" encoding="utf-8" ?> <xs:schema id="Test" targetNamespace="http://tempuri.org/Test.xsd" elementFormDefault="qualified" attributeFormDefault="qualified" xmlns="http://tempuri.org/Test.xsd" xmlns:mstns="http://tempuri.org/Test.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="Test" msdata:IsDataSet="true"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="TableOne"> <xs:complexType> <xs:sequence> <xs:element name="TableOneID" type="xs:integer" /> <xs:element name="TableOneDesc" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="TableTwo"> <xs:complexType> <xs:sequence> <xs:element name="TableTwoID" type="xs:integer" minOccurs="0" /> <xs:element name="TableTwoDesc" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="TableOne_TableTwo_Relationship"> <xs:complexType> <xs:sequence> <xs:element name="TableOne_TableTwo_RelationshipID" type="xs:integer" minOccurs="0" /> <xs:element name="TableOneID" type="xs:integer" minOccurs="0" /> <xs:element name="TableTwoID" type="xs:integer" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:key name="TableOnePrimaryKey" msdata:PrimaryKey="true"> <xs:selector xpath=".//mstns:TableOne" /> <xs:field xpath="mstns:TableOneID" /> </xs:key> <xs:key name="TableTwoID" msdata:PrimaryKey="true"> <xs:selector xpath=".//mstns:TableTwo" /> <xs:field xpath="mstns:TableTwoID" /> </xs:key> <xs:key name="TableOne_TableTwo_RelationShipPrimaryKey" msdata:PrimaryKey="true"> <xs:selector xpath=".//mstns:TableOne_TableTwo_Relationship" /> <xs:field xpath="mstns:TableOne_TableTwo_RelationshipID" /> </xs:key> <xs:keyref name="TableOneTableOne_TableTwo_Relationship" refer="TableOnePrimaryKey"> <xs:selector xpath=".//mstns:TableOne_TableTwo_Relationship" /> <xs:field xpath="mstns:TableOneID" /> </xs:keyref> <xs:keyref name="TableTwoTableOne_TableTwo_Relationship" refer="TableTwoID"> <xs:selector xpath=".//mstns:TableOne_TableTwo_Relationship" /> <xs:field xpath="mstns:TableTwoID" /> </xs:keyref> </xs:element> </xs:schema>Good luck!