Array Contents to XML

  • Thread starter Thread starter Steve Bishop
  • Start date Start date
S

Steve Bishop

Could anyone give me an example of sending an array's variables to an
XML document all within a subroutine. I'm thinking of a basic example
capturing a form fields values into the array. Help appreciated.

Thanks
 
let the Framework do the work! Load your Array into a DataTable, the DataTable into a DataSet, and use WriteXML..guess I'm just lazy..

here is a quick and dirty:


private void ArrayToXML(object [] YourArray)

{

try

{

DataTable dt = new DataTable("SomeTable");

dt.Columns.Add("ArrayElements");


foreach (object o in YourArray)

{

DataRow dr = dt.NewRow();

dr["ArrayElements"] = o.ToString();

dt.Rows.Add(dr);

}


DataSet ds = new DataSet();

ds.Tables.Add(dt);

ds.WriteXml("C:\\Somefile.xml"); //probably change this

}

catch (Exception ex)

{

//your handling

}

}


Some Inputs :
object [] myArray = {"Hello","this","should","work"};
ArrayToXML(myArray);



will give:

<?xml version="1.0" standalone="yes" ?>
- <NewDataSet>
- <SomeTable>
<ArrayElements>Hello</ArrayElements>
</SomeTable>
- <SomeTable>
<ArrayElements>this</ArrayElements>
</SomeTable>
- <SomeTable>
<ArrayElements>should</ArrayElements>
</SomeTable>
- <SomeTable>
<ArrayElements>work</ArrayElements>
</SomeTable>
</NewDataSet>

Of course you can tweak it as you like...
 
Back
Top