Help with DataRelationCollection indexer

  • Thread starter Thread starter Jeff Higgins
  • Start date Start date
J

Jeff Higgins

In the code below, when I use the integer indexer I get the correct output.

DataRelation myRelation = ds.Tables["TestDocument"].ChildRelations[0];
Output:
No.1TestItems
No.2TestItems

But when I use the string indexer the output is nil.

DataRelation myRelation =
ds.Tables["TestDocument"].ChildRelations["RelationDocumentItems"];
Output:

Will someone help me understand what I'm doing wrong?
Thanks,
Jeff Higgins


using System;
using System.Data;
using System.Xml;
using System.Text;
using System.Windows.Forms;

namespace xmltest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StringBuilder s = new StringBuilder();

DataSet ds = new DataSet();
ds.ReadXml("test.xml", XmlReadMode.Auto);

DataRelation myRelation =
ds.Tables["TestDocument"].ChildRelations["RelationDocumentItems"];
DataTable myTable = ds.Tables["TestDocument"];
DataRow[] arrRows;
foreach(DataRow myRow in myTable.Rows)
{
arrRows = myRow.GetChildRows(myRelation, DataRowVersion.Current);
for(int i = 0; i < arrRows.Length; i++)
{
s.Append(arrRows["name"] + "\n");
}
}
MessageBox.Show(s.ToString(), "Menu Data", MessageBoxButtons.OK);
}
}
}


<?xml version="1.0" encoding="utf-8"?>
<TestDocument xmlns="http://tempuri.org/test.xsd"
me="{E292E134-BEC8-49f7-A1E8-C3580C76C5B8}" name="TestDocument">
<TestItems me="{3738B467-C142-4ece-88B6-666327FA37B0}"
owner="{E292E134-BEC8-49f7-A1E8-C3580C76C5B8}" name="No.1TestItems">
<TestItem me="{72C372F3-4E96-4a82-892B-B441E42D77B6}"
owner="{3738B467-C142-4ece-88B6-666327FA37B0}" name="No.1FirstTestItem" />
<TestItem me="{BEB920DF-C2BA-46cf-8E59-EB060E2715DD}"
owner="{3738B467-C142-4ece-88B6-666327FA37B0}" name="No.1SecondTestItem" />
<TestItem me="{3776230C-0669-4be4-BBFB-1D50789320D4}"
owner="{3738B467-C142-4ece-88B6-666327FA37B0}" name="No.1ThirdTestItem" />
</TestItems>
<TestItems me="{560223A6-0938-4a3b-89CD-9507F1F26682}"
owner="{E292E134-BEC8-49f7-A1E8-C3580C76C5B8}" name="No.2TestItems">
<TestItem me="{2AA34205-3695-4f2c-9DF0-C3D0EA9E1AF2}"
owner="{560223A6-0938-4a3b-89CD-9507F1F26682}" name="No.2FirstTestItem" />
<TestItem me="{3EC22224-B0BE-4364-A631-F455AD2E6F61}"
owner="{560223A6-0938-4a3b-89CD-9507F1F26682}" name="No.2SecondTestItem" />
<TestItem me="{8FC33219-4104-458e-A08A-8A5DB549DD50}"
owner="{560223A6-0938-4a3b-89CD-9507F1F26682}" name="No.2ThirdTestItem" />
</TestItems>
</TestDocument>


<?xml version="1.0" ?>
<xs:schema id="NewDataSet" targetNamespace="http://tempuri.org/test.xsd"
xmlns:mstns="http://tempuri.org/test.xsd"
xmlns="http://tempuri.org/test.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="TestDocument">
<xs:complexType>
<xs:sequence>
<xs:element name="TestItems" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="TestItem" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="me" form="unqualified" type="xs:string" />
<xs:attribute name="owner" form="unqualified" type="xs:string" />
<xs:attribute name="name" form="unqualified" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="me" form="unqualified" type="xs:string" />
<xs:attribute name="owner" form="unqualified" type="xs:string" />
<xs:attribute name="name" form="unqualified" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="me" form="unqualified" type="xs:string" />
<xs:attribute name="name" form="unqualified" type="xs:string" />
</xs:complexType>
<xs:key name="KeyPrimaryTestDocument" msdata:PrimaryKey="true">
<xs:selector xpath="." />
<xs:field xpath="@me" />
</xs:key>
<xs:key name="KeyPrimaryTestItems" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:TestItems" />
<xs:field xpath="@me" />
</xs:key>
<xs:key name="KeyPrimaryTestItem" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:TestItem" />
<xs:field xpath="@me" />
</xs:key>
<xs:keyref name="RelationDocumentItems"
refer="mstns:KeyPrimaryTestDocument">
<xs:selector xpath=".//mstns:TestItems" />
<xs:field xpath="@owner" />
</xs:keyref>
<xs:keyref name="RelationItemsItem" refer="mstns:KeyPrimaryTestItems">
<xs:selector xpath=".//mstns:TestItem" />
<xs:field xpath="@owner" />
</xs:keyref>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true"
msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="TestDocument" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
 
If I do:
DataRelation myRelation = ds.Tables["TestDocument"].ChildRelations[0];
s.Append(myRelation.ToString());

I get in s:
TestDocument_TestItems

rather than the expected:
RelationDocumentItems

Where does that come from?
Thanks,
Jeff Higgins
 
Maybe my schema is not being read, instead ReadXml() is infering a schema?

Any help will much appreciated.
Thanks
Jeff Higgins
 
Thank you, Addison Wesley Professional and Shawn Wildermuth

for the sample chapter titled Typed DataSets in ADO.NET
from your book titled Pragmatic ADO.NET: Data Access for the Internet World
ISBN: 0201745682

located at:

http://www.awprofessional.com/articles/article.asp?p=30593


Thank you,

Addison Wesley Professional and Bob Beauchemin

for the sample chapter titled Essential ADO.NET: XML and Data Access
Integration
from your book titled Essential ADO.NET ISBN: 0201758660

and located at:

http://www.awprofessional.com/articles/article.asp?p=29901
 
Back
Top