Saving DataTable as XML

P

Praveen

Hi All,
I was looking for a way to save the Different DataTables in a dataset as
differnt XML files. The WriteXML function is for Dataset and it saves all
the dataTables into one file. Is there any thing ready made.

Thanks in advance,
Praveen
 
V

vinu

Praveen

No readymade method to save tables in dataset as individual xml file
DataTable has WriteXML method. Here is sample code written in .net 2 . To
get all the tables in dataset use for each statement and loop through
DataTable collection in DataSet

SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();

SqlDataAdapter adap = new SqlDataAdapter();
SqlDataReader rdr ;

con.ConnectionString =
@"server=.;database=AdventureWorks;uid=xx;pwd=xx";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"select LocationID,Name as LocNAme from
Production.Location";
adap.SelectCommand = cmd;
adap.Fill(ds, "ER");
ds.Tables[0].WriteXml(@"c:\t.xml");

Vinu
 
P

Praveen

Hi Vinu,
I dont find WriteXml available at the dataTable. I am using dotnet framework
v1.1.4322 is this only available in the .net version 2?

Thanks,
Praveen

vinu said:
Praveen

No readymade method to save tables in dataset as individual xml file
DataTable has WriteXML method. Here is sample code written in .net 2 . To
get all the tables in dataset use for each statement and loop through
DataTable collection in DataSet

SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();

SqlDataAdapter adap = new SqlDataAdapter();
SqlDataReader rdr ;

con.ConnectionString =
@"server=.;database=AdventureWorks;uid=xx;pwd=xx";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"select LocationID,Name as LocNAme from
Production.Location";
adap.SelectCommand = cmd;
adap.Fill(ds, "ER");
ds.Tables[0].WriteXml(@"c:\t.xml");

Vinu


Praveen said:
Hi All,
I was looking for a way to save the Different DataTables in a dataset as
differnt XML files. The WriteXML function is for Dataset and it saves all
the dataTables into one file. Is there any thing ready made.

Thanks in advance,
Praveen
 
C

Cor Ligthert [MVP]

Praveen,

It is in Net 2.0, however than is still as a dataset written.

In fact it is nothing more than this.

I don't know what your language is, however I follow vinu

\\\
DataTable dt = ds.Tables[1].Copy(); //a copy of table 2.
DataSet ds2 = new DataSet();
ds2.Tables.Add(dt);
ds2.WriteXML(path);
///

Typed here in the message so watch typos

I hope this helps

Cor

Praveen said:
Hi Vinu,
I dont find WriteXml available at the dataTable. I am using dotnet
framework v1.1.4322 is this only available in the .net version 2?

Thanks,
Praveen

vinu said:
Praveen

No readymade method to save tables in dataset as individual xml file
DataTable has WriteXML method. Here is sample code written in .net 2 . To
get all the tables in dataset use for each statement and loop through
DataTable collection in DataSet

SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();

SqlDataAdapter adap = new SqlDataAdapter();
SqlDataReader rdr ;

con.ConnectionString =
@"server=.;database=AdventureWorks;uid=xx;pwd=xx";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"select LocationID,Name as LocNAme from
Production.Location";
adap.SelectCommand = cmd;
adap.Fill(ds, "ER");
ds.Tables[0].WriteXml(@"c:\t.xml");

Vinu


Praveen said:
Hi All,
I was looking for a way to save the Different DataTables in a dataset as
differnt XML files. The WriteXML function is for Dataset and it saves
all the dataTables into one file. Is there any thing ready made.

Thanks in advance,
Praveen
 
P

Praveen

Thanks Cor,
The code below would work but there is a copy of the dataTable. In my case
the data would be huge so I did not want to take a copy for obvious reasons.
If I dont find anything else then i would have to live with this.

Regards,
Praveen


Cor Ligthert said:
Praveen,

It is in Net 2.0, however than is still as a dataset written.

In fact it is nothing more than this.

I don't know what your language is, however I follow vinu

\\\
DataTable dt = ds.Tables[1].Copy(); //a copy of table 2.
DataSet ds2 = new DataSet();
ds2.Tables.Add(dt);
ds2.WriteXML(path);
///

Typed here in the message so watch typos

I hope this helps

Cor

Praveen said:
Hi Vinu,
I dont find WriteXml available at the dataTable. I am using dotnet
framework v1.1.4322 is this only available in the .net version 2?

Thanks,
Praveen

vinu said:
Praveen

No readymade method to save tables in dataset as individual xml file
DataTable has WriteXML method. Here is sample code written in .net 2 .
To get all the tables in dataset use for each statement and loop through
DataTable collection in DataSet

SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();

SqlDataAdapter adap = new SqlDataAdapter();
SqlDataReader rdr ;

con.ConnectionString =
@"server=.;database=AdventureWorks;uid=xx;pwd=xx";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"select LocationID,Name as LocNAme from
Production.Location";
adap.SelectCommand = cmd;
adap.Fill(ds, "ER");
ds.Tables[0].WriteXml(@"c:\t.xml");

Vinu


Hi All,
I was looking for a way to save the Different DataTables in a dataset
as differnt XML files. The WriteXML function is for Dataset and it
saves all the dataTables into one file. Is there any thing ready made.

Thanks in advance,
Praveen
 
K

Kevin Yu [MSFT]

Hi Praveen,

I agree with Cor that we have to make a copy of it and add the copy to
another DataSet. Or we'll have to detach the DataTable from the origianl
DataSet, which would affect that DataSet.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
P

Praveen

Hi Kevin,
I am ok if I can detach the DataTable from the initial DataSet and attach to
a new DataSet. In the specific case where I am using this it does not pose
any problems as i would anyway discard the original DataSet.

could you please let me know how to detach a dataTable from one dataset and
attach to another. Also what would happen of the schema that I would have
imported to the previous dataset. would that also be moved to the new
DataSet ?

Thanks in advance,
Praveen
 
C

Cor Ligthert [MVP]

Praveen,

I hope you don't mind that I answer this.

I had it first as sample with the remove

ds.Tables.Remove(dt);
or
ds.Tables.RemoveAt(0);

What I don't know is if the relations will than be removed as well, that was
the reason I did not give it you direct. (If you have no relations than that
is of course not important)

Adding it again will than be
ds.Tables.Add(dt);

(be aware that it will be added at the end then).

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top