How to load XML string into DataGrid and associate a element attribute to a column?

  • Thread starter Thread starter Vinícius Pitta Lima de Araújo
  • Start date Start date
V

Vinícius Pitta Lima de Araújo

Hi,
I have a query return in a XML string. My XML result looks like:
<activities>
<activity>
<subject>Win the Nobel</subject>
<scheduledstart date="01/01/2005"
time="00:00">2005-01-01T00:00:00-3:00</scheduledstart>
</activity>
</activities>

If a dont have the scheduledstart element, the loading of result into a
DataSet works fine, but when I try put the element and associate it with a
column I get a error (something like "field scheduledstart not found").
See my code:

NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
XmlParserContext context = new XmlParserContext(null, nsmgr, null,
XmlSpace.None);
XmlTextReader tr = new XmlTextReader(strResultsXml, XmlNodeType.Document,
context);
DataSet ds = new DataSet();
ds.ReadXml(tr);

BoundColumn colAssunto = new BoundColumn();
colAssunto.HeaderText = "Assunto";
colAssunto.DataField = "subject";

BoundColumn colInicio = new BoundColumn();
colInicio.HeaderText = "Inicio";
colInicio.DataField = "scheduledstart";

dgResultado.AutoGenerateColumns = false;
dgResultado.Columns.Add(colAssunto);
dgResultado.Columns.Add(colInicio);
dgResultado.DataSource = ds;

if (ds.HasChanges())
dgResultado.DataBind();

How I associate the scheduledstart date and time attributes to two columns
or the content of the element to one column?
Can somebody help me?

Thank you for pay attention and sorry my bad english. :)

[]'s
Vinícius Pitta Lima de Araújo
 
The reason it is failing is due to the use of a DataReade

Simply edit the column name of the DataSet to match that of the XM

O

Change the code t

BoundColumn colAssunto = new BoundColumn()
colAssunto.HeaderText = "Assunto"
colAssunto.DataField = 0

BoundColumn colInicio = new BoundColumn()
colInicio.HeaderText = "Inicio"
colInicio.DataField = 1
 
Thank you for your reply, Ian.
What is the rigth column name who match the xml element? If the name of the
element is "scheduledstart" the value of colInicio.DataField must be
"scheduledstart", right? The DataField attribute is a string and can't be
assigned with a int value.

[]'s
Vinícius Pitta Lima de Araújo
 
I discovered who the dataset are loading the data in two tables as follow:
Table: activity
Columns: activitiyid, subject, activity_Id

Table: scheduledstart
Columns: date, time, scheduledstart_Text, activity_Id

The problem is: The datagrid display only one Table of the dataset. How to
display columns from two tables?

[]'s
Vinícius Pitta Lima de Araújo

Vinícius Pitta Lima de Araújo said:
Thank you for your reply, Ian.
What is the rigth column name who match the xml element? If the name of the
element is "scheduledstart" the value of colInicio.DataField must be
"scheduledstart", right? The DataField attribute is a string and can't be
assigned with a int value.

[]'s
Vinícius Pitta Lima de Araújo

Ian said:
The reason it is failing is due to the use of a DataReader

Simply edit the column name of the DataSet to match that of the XML

Or

Change the code to

BoundColumn colAssunto = new BoundColumn();
colAssunto.HeaderText = "Assunto";
colAssunto.DataField = 0;

BoundColumn colInicio = new BoundColumn();
colInicio.HeaderText = "Inicio";
colInicio.DataField = 1;
 
Vinícius

The reason that you can not bind to the XML element "scheduledstart" is due to the attributes held on the element. If you remove the attributes so that it looks like this, then it work

<activities><activity><subject>Win the Nobel</subject><scheduledstart>2005-01-01T00:00:00-3:00</scheduledstart></activity></activities

I am trying to figure out why!
 
I cant remove the attributes of the element. Now I know who the dataset load
the data into two tables: activity and scheduledstart connected by
activity_Id (looks like a autommatic field generated by the dataset).

The trouble is who the datagrid show only one table. I am trying show
columns from the two tables without sucess. Can you help me? :)

Thank you one more time.

[]'s
Vinícius Pitta Lima de Araújo

Ian said:
Vinícius

The reason that you can not bind to the XML element "scheduledstart" is
due to the attributes held on the element. If you remove the attributes so
that it looks like this, then it works
 
It can looks like a joke, but I searche and not found how to do it. I find
a lot of samples who describe how use a relation, how create a child view
for a row and more but not found what I want!

I try use DataView, DataViewManager, construct other DataSet using the
columns who I want display (I dont know how load the data into the new
dataset from the source dataset), and a bit of solutions who I can imagine.

Is possible who nobody was tried or need to do what I want do? I just want
join two tables in one on a dataset to display in a datagrid.

I am newbie in dotnet programming (begin on last week) but I have some
experience and skills in general programming and I am loosing a bit of time
trying do this, who I think is very simple...

Can you help me (one more time. :) )? Can somebody help me?!

Thank you and sorry my bad english.

[]'s
Vinícius Pitta Lima de Araújo

Ian said:
Reason, if you have an xml document which contains elements that have
attributes, these will be determined to be tables, hence if you try the
following only, you will see the results for scheduledstart
dgResultado.AutoGenerateColumns = true;
dgResultado.DataSource = ds.Tables[1].DefaultView;

If an element that is inferred as a table has a child element that is also
inferred as a table, a DataRelation will be created between the two tables,
in our case a new column with a name of "activity_Id" will be added.
Essentially, what you will need to do is create a view of the data using
the DataViewManager, i think
 
Finally I obtained the desired result. The solution it was to make manually
the join of the two tables. I believe that is not the best way but solve my
problem for while.

/*
* This method copy the rows values for the columns who will be displayed
* This method was written to solve my needs, so is not a generic method who
will works
* in any situation. If you need do the same thing and this explaination
dont help you go to:
* http://support.microsoft.com/?id=326080#0
*
* At this time I asume who the dataset is already loaded with the data,
tables and relations
*/
public DataTable criarTabelaUnica(DataSet ds) {
DataTable tabela = new DataTable("Atividade"); //the return table with a
name choose by me. :P

tabela.Columns.Add(new DataColumn("AtividadeId")); // = activityid
tabela.Columns.Add(new DataColumn("Assunto")); // = subject
tabela.Columns.Add(new DataColumn("Inicio")); // = date

DataRow[] linhas = ds.Tables["scheduledstart"].Select(); //the rows of
child table of the relationship

foreach (DataRow origem in linhas) {
DataRow destino = tabela.NewRow(); //the destin row who will be
added to the new table
DataRow atividade = origem.GetParentRow(ds.Relations[0]); //get the
parent row

//fill the fields
destino["AtividadeId"] = atividade["activityid"];
destino["Assunto"] = atividade["subject"];
destino["Inicio"] = origem["date"];

tabela.Rows.Add(destino); //add the recem-created row to the result
table
}

return tabela; //return the table and that is all folks. Now you need
only load the table into a dataset, dataview, etc and bind to the datagrid
}

Thank you Ian for your help, attention and be patient.
PS.: If you are curiouss, the code is in portuguese. I am from Brazil. ;)

[]'s
Vinícius Pitta Lima de Araújo


Vinícius Pitta Lima de Araújo said:
It can looks like a joke, but I searche and not found how to do it. I find
a lot of samples who describe how use a relation, how create a child view
for a row and more but not found what I want!

I try use DataView, DataViewManager, construct other DataSet using the
columns who I want display (I dont know how load the data into the new
dataset from the source dataset), and a bit of solutions who I can imagine.

Is possible who nobody was tried or need to do what I want do? I just want
join two tables in one on a dataset to display in a datagrid.

I am newbie in dotnet programming (begin on last week) but I have some
experience and skills in general programming and I am loosing a bit of time
trying do this, who I think is very simple...

Can you help me (one more time. :) )? Can somebody help me?!

Thank you and sorry my bad english.

[]'s
Vinícius Pitta Lima de Araújo

Ian said:
Reason, if you have an xml document which contains elements that have
attributes, these will be determined to be tables, hence if you try the
following only, you will see the results for scheduledstart
dgResultado.AutoGenerateColumns = true;
dgResultado.DataSource = ds.Tables[1].DefaultView;

If an element that is inferred as a table has a child element that is
also
inferred as a table, a DataRelation will be created between the two tables,
in our case a new column with a name of "activity_Id" will be added.
Essentially, what you will need to do is create a view of the data using
the DataViewManager, i think
 
Vinícius

Glad to hear you got a solution, it seems a bit odd that a DataView can only be on one table and not across multiple tables, as you would see a traditional view in SQL server

I also found the solution you came up with on Google this morning

All the bes
Ian
 
Thank you for you help, Ian.
I loose a lot of time looking for a done solution.

"The smallest distance between two points is a line..."

[]'s
Vinícius Pitta Lima de Araújo

Ian said:
Vinícius

Glad to hear you got a solution, it seems a bit odd that a DataView can
only be on one table and not across multiple tables, as you would see a
traditional view in SQL server.
 
Back
Top