Menu
Home
About Me
Articles
Hobbies
Code Library
Flash Library
3d stuido max

 
For now, let us pretend that this is the data returned and stored to a dataset (ds)
GenderQuantity
Male10
Female20
using System.Xml;
using System.Text;

Response.ContentType = "text/xml";
// Create a new XmlTextWriter instance
XmlTextWriter writer = new 	XmlTextWriter(Response.OutputStream, Encoding.UTF8);

// start writing!
writer.WriteStartDocument();
	writer.WriteStartElement("Results");
		writer.WriteStartElement("Breakdown");
		foreach(DataRow row in ds.Tables[0].Rows)
		{
			writer.WriteStartElement("", row["Gender"].ToString(), "");
			writer.WriteString(row["Quantity"].ToString());
			writer.WriteEndElement();
		}
		writer.WriteEndElement();
	writer.WriteEndElement(); //end Results
writer.WriteEndDocument();
writer.Close();
Would return, as an XML document,
	
-<Results>
  -<Breakdown>
    <Male>10</Male>
    <Female>20</Female>
  </Breakdown>
</Results>
Lets read the same XML into a DataSet
string URL = "http://www.mysite.com/somexml.xml";
System.Net.HttpWebRequest wreq = 
	(System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);
wreq.Method = "GET";
wreq.Timeout = 3000;
System.Net.HttpWebResponse wr = (System.Net.HttpWebResponse)wreq.GetResponse();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(wr.GetResponseStream());
XmlElement objXML_Root = xmldoc.DocumentElement;

XmlNodeList BreakDownNodes =
	xmldoc.DocumentElement.ChildNodes[1].ChildNodes;

DataSet ds = new DataSet();
// Create new DataTable and DataSource objects.
DataTable myDataTable = new DataTable();
// Declare DataColumn and DataRow variables.
DataColumn myColumn;
DataRow myRow;
// Create new DataColumn, set DataType, ColumnName and add to DataTable.
myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.Int32");
myColumn.ColumnName = "Quantity";
myDataTable.Columns.Add(myColumn);

// Create second column.
myColumn = new DataColumn();
myColumn.DataType = Type.GetType("System.String");
myColumn.ColumnName = "Gender";
myDataTable.Columns.Add(myColumn);

for (int x = 0;x<BreakDownNodes.Count;x++)
{
	myRow = myDataTable.NewRow();
	myDataTable.Rows.Add(myRow);
	myDataTable.Rows[x][0] = BreakDownNodes.Item(x).InnerText;
	myDataTable.Rows[x][1] = BreakDownNodes.Item(x).Name;
}
ds.Tables.Add(myDataTable);

//Tada one DataSet