How to set Attribute to DataSet?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I am creating DataSet and many DataTables and populating value to those
tables and finally writing the values as XML.
I want to add attribute to my DataSet. How can I do this?

DataSet objDataSet = new DataSet("Test");
objDataSet.WriteXml(@"C:\Test.xml");

Output:
<?xml version="1.0" standalone="yes" ?>
<Test />

I want in the below given format:
<?xml version="1.0" standalone="yes" ?>
<Test Version="3.1" />

One more question: Is it possible to eliminate "<?xml version="1.0"
standalone="yes" ?> " when I use DataSet to write the XML file?

thanks

With Regards
Venkat
 
You can specify the namespace property:
DataSet ds = new DataSet("CuckooDataSet");
ds.Namespace = "BillRyan";

DataTable dt = new DataTable("CuckooTable");

DataColumn dc = new DataColumn("CuckooName", typeof(System.String));

dt.Columns.Add(dc);

ds.Tables.Add(dt);

DataRow dro = dt.NewRow();

dro[0] = "BillRyan";

dt.Rows.Add(dro);

dro = dt.NewRow();

dro[0] = "Aishwarya Ray";

dt.Rows.Add(dro);

ds.WriteXml(@"C:\Cuckooz.xml");

Which will add a namespace for you. This should get you what you want unless you specifically need a named attribute:

<?xml version="1.0" standalone="yes"?>
<CuckooDataSet xmlns="BillRyan">
<CuckooTable>
<CuckooName>BillRyan</CuckooName>
</CuckooTable>
<CuckooTable>
<CuckooName>Aishwarya Ray</CuckooName>
</CuckooTable>
</CuckooDataSet>



Now, if you want to modify the XML, you can use XSLT to do so and you have 100% control over everything there. Similarly, you can tinker with the ExtendedProperties and add wahtever extraneious information that you might want. Are you familiar with XSLT? If not, let me know and I'll walk you through it.


Cheers,



Bill
 
Hi Rayan,

Thanks for the info. I want to add the version No. in the root node. ie
<CuckooDataSet xmlns="BillRyan" Version="3.0"> How can I do this?.
I am new to XSLT and you are most welcome to let me know more info about it.

Thanks in advance

With Regards
Venkat


W.G. Ryan MVP said:
You can specify the namespace property:
DataSet ds = new DataSet("CuckooDataSet");
ds.Namespace = "BillRyan";

DataTable dt = new DataTable("CuckooTable");

DataColumn dc = new DataColumn("CuckooName", typeof(System.String));

dt.Columns.Add(dc);

ds.Tables.Add(dt);

DataRow dro = dt.NewRow();

dro[0] = "BillRyan";

dt.Rows.Add(dro);

dro = dt.NewRow();

dro[0] = "Aishwarya Ray";

dt.Rows.Add(dro);

ds.WriteXml(@"C:\Cuckooz.xml");

Which will add a namespace for you. This should get you what you want unless you specifically need a named attribute:

<?xml version="1.0" standalone="yes"?>
<CuckooDataSet xmlns="BillRyan">
<CuckooTable>
<CuckooName>BillRyan</CuckooName>
</CuckooTable>
<CuckooTable>
<CuckooName>Aishwarya Ray</CuckooName>
</CuckooTable>
</CuckooDataSet>



Now, if you want to modify the XML, you can use XSLT to do so and you have 100% control over everything there. Similarly, you can tinker with the ExtendedProperties and add wahtever extraneious information that you might want. Are you familiar with XSLT? If not, let me know and I'll walk you through it.


Cheers,



Bill





Venkatachalam said:
Hi,
I am creating DataSet and many DataTables and populating value to those
tables and finally writing the values as XML.
I want to add attribute to my DataSet. How can I do this?

DataSet objDataSet = new DataSet("Test");
objDataSet.WriteXml(@"C:\Test.xml");

Output:
<?xml version="1.0" standalone="yes" ?>
<Test />

I want in the below given format:
<?xml version="1.0" standalone="yes" ?>
<Test Version="3.1" />

One more question: Is it possible to eliminate "<?xml version="1.0"
standalone="yes" ?> " when I use DataSet to write the XML file?

thanks

With Regards
Venkat
 
Back
Top