Import XML file into dataset

  • Thread starter Thread starter Thomas Lischetzki
  • Start date Start date
T

Thomas Lischetzki

Hello all together,

I have a little problem importing the following xml file into a dataset.

<?xml version="1.0" standalone="yes"?>
<logfile version = "0.1.001" >
<log date = "01.01.2005" time = "12:01:59" username = "Mueller" level =
"Info" message = "2">
<data order="1" caption="Original Value">60</data>
<data order="2" caption="Changed Value">80</data>
</log>
</logfile>

If I load the xml file in Visual Studio and looks at the data page I see 3
data tables.

1. logfile
2. log
3. data

If I choose the table 'log' it shows all information which are stored in the
tag 'log'.
Now I can expand a datarow and gets a link 'log data'.
If I click this link I get a new table with the information stored in the
tags 'data'.

That's fine, but how can I get access to the information within my code?
Hope you understand my problem and can help me.

Best regards
Thomas
 
Thomas,

And XML dataset contains only elements. You have an XML document.
That you can only import in a dataset by reading it looping while using the
xmlnodereader to get attribute by attribute to set those in the dataset
elements (items).

I hope this gives an idea

Cor
 
Hello Cor,

Hmm Visual Studio displays the XML file in a dataset with 2 layers (don't
know how to describe it better).
The first layer is a table which contains all information from the tag
'log'.
That means I have a table which contains rows like this one (according to my
example):

01.01.2005 12:01:59 Mueller Info 2

This rows have a plus, so I can expand it.
If I expand the row, I get a link to the second layer or better a second
table which contains the information of the tag 'data'.
The problem is that I don't have access to the second layer within my code.
I get the first layer (table) if I use the readXml-method of the dataset but
not the second layer (table).

Hope you understand what I mean.

Best regards
Thomas
 
Thomas,

You can read it as dataset.

To show it, you can use the designer or this simple method.
\\\
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml("XMLFile1.xml");
this.dataGrid1.DataSource = ds;
this.dataGrid1.Expand(-1);
///

I hope this helps?

Cor
 
Hello Cor,

that is not my problem.
I know that the designer shows me the XML file as a dataset.
You can see in my posting sent 24.02.2005 12:04, what my problem with this
representation is.
I need to get access to the secon layer and the information stored in the
'data' tag.

Best regards
Thomas
 
Thomas,

That was why I showed this sample, when I add this
\\\
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml("XMLFile1.xml");
this.dataGrid1.DataSource = ds;
this.dataGrid1.Expand(-1);
this.textBox1.DataBindings.Add("Text",ds.Tables[1],"username");
///

Than I see "Mueller" in the textbox on the form.

It has an other effect that I don't like however it is because of this
sample because the field name what is than

ds.Tables["log"]["username"]

I was expecting you understood that.

I hope this helps?

Cor
 
Hello Cor,

ok so I get access to the information stored in the tag 'log'.
I understood your example, but I don't get access to the information stored
in the tag 'data', because this information are stored in another table I
think or a table with a filter?!?
That is my main problem.

Thanks already for your time.

Best regards
Thomas
 
Thomas,

Did you try my sample,

I get with this in the datagrid exactly what I expected.
(I see now that my too quick typed datafield was wrong however the method
stays the same)
{
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml("XMLFile1.xml");
ds.Tables["data"].Rows[0]["caption"] = "Hello I am there";
this.dataGrid1.DataSource = ds;
this.dataGrid1.Expand(-1);
}

I hope this helps?

Cor
 
Hi Cor,

yes, your example shows me the XML file in a dataset like the designer does.
If I access the table 'data' with this code line:

ds.Tables["data"].Rows[0]["caption"] = "Hello I am there";

it works fine, but I don't have a relation to the related datarow in the
table 'log', did I?
The dataset in your example also shows me the link to the table 'data' if I
expand a datarow in the table 'log'.
But I can't access this filtered view of the table 'data'.

Best regards
Thomas
 
Hi Cor,

yes, your example shows me the XML file in a dataset like the designer does.
If I access the table 'data' with this code line:

ds.Tables["data"].Rows[0]["caption"] = "Hello I am there";

it works fine, but I don't have a relation to the related datarow in the
table 'log', did I?
The dataset in your example also shows me the link to the table 'data' if I
expand a datarow in the table 'log'.
But I can't access this filtered view of the table 'data'.

Best regards
Thomas
 
Thomas,

You mean this one, that is just playing with the relations.
\\\\
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml("XMLFile1.xml");
ds.Tables["data"].Rows[0]["caption"] = "Hello I am there";
ds.Tables["log"].ChildRelations[0].ChildTable.Rows[1]["caption"] = "I too";
this.dataGrid1.DataSource = ds;
this.dataGrid1.Expand(-1);
///

I hope it becomes clearer?

Cor
 
Hi Cor,

many thanks to you.

The following sample code delivers me the needed information:

System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml("log_2-2005.xml");

foreach (DataRow dr in ds.Tables["log"].Rows)
{
System.Windows.Forms.MessageBox.Show(dr["level"].ToString() + ", " +
dr["message"].ToString());
DataRow[] childs = dr.GetChildRows("log_data");
foreach(DataRow dr2 in childs)
{
System.Windows.Forms.MessageBox.Show(dr2["data_text"].ToString());
}
}

Thanks for the spent time in my problem.
I think now I understand the dataset much better.

Best regards
Thomas
 
Back
Top