Groups | Blog | Home
all groups > dotnet xml > november 2004 >

dotnet xml : DataGrid support for XML with different node types


yurps NO[at]SPAM yahoo.co.uk
11/3/2004 5:46:11 AM
Hello,

I have xml like this....

<test>
<question>sdfsa</question>
<section><question>43ga</question>
<question>asdf</question>
</test>

I make and xsd file with
<xs:all>
<xs:element....
<xs:element....
</xs:all>

I then bind the XML to a DataGrid...

DataGrid.DataSource = DataSet;
DataGrid.DataBind();

but I only get the questions, I notice there is a DataMember property
I can set to DataGrid.DataMember = "Section"; and I will get that one,
but essential I want a table with both elements in there...

Is there anyway to do this...?

Nigel Armstrong
11/7/2004 10:09:07 AM
Hi Magister

Try creating a DataSet in Visual Studio .NET using the design time
environment - you will see that if you create an element, then add a couple
of 'fields' to it, then the structure you get in the xsd looks a bit like
this:

<?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:all>
<xs:element name="Test">
<xs:complexType>
<xs:sequence>
<xs:element name="Question" type="xs:string" minOccurs="0" />
<xs:element name="Answer" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>

Then you can bind to the Question and Answer 'columns' by setting the
DataSource. Essentially the XML data needs to look more table like to work
the way that you want, with a root element representing the table, top level
element children of the root representing rows, and grand children of the
root representing columns.

HTH

Nigel

[quoted text, click to view]
yurps NO[at]SPAM yahoo.co.uk
11/8/2004 3:53:35 AM
Thanks for getting back to me, the thing is that I am binding to a
hierarchical grid control....I am using attributes as columns...

<test>
<question Type="Selection" Text="What is the capital of the USA">
<option Text="Texas" />
<option Text="DC" />
</question>
<section Type="Random" Text="Here is a section about France" >
<question Type="Selection" Text="What is the capital of the
France">
<option Text="Paris" />
<option Text="Lyon" />
</question>
<question Type="Input" Text="Type in the major language spoken in
France">
</section>
<question Type="Input" Text="Type in the major language spoken in
the USA">
<question Type="Selection" Text="What is the capital of the UK">>
<option Text="London" />
<option Text="Manchester" />
</question>
</test>

I wanteded to bind the columns of question and section in the first
level of the hierarchical grid as they have similar attribute columns,
but when I get my typed Dataset Section and Question are 2 different
tables...how can I put them together so they are one...?

Thanks,
Matt

[quoted text, click to view]
AddThis Social Bookmark Button