public void XmlStringDataSet()
{
DataSet ds = new DataSet();
StringReader sr = null;
XmlTextReader xr = null;
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"C:\Documents and Settings\aakash\Desktop\toc.xml");
StringBuilder s = null;
sr = new StringReader(xdoc.InnerXml);
xr = new XmlTextReader(sr);
ds.ReadXml(xr);
foreach (DataTable t in ds.Tables)
{
Console.WriteLine("Table:" + t.ToString() + "\n");
foreach (DataRow r in t.Rows)
{
s = new StringBuilder();
foreach (DataColumn c in t.Columns)
{
s.Append(r[c]);
s.Append(" : ");
}
Console.WriteLine("Row: " + s.ToString() + "\n");
}
}
}
public string DataSetToXml()
{
DataSet ds = new DataSet();
ds.ReadXml(@"C:\Documents and Settings\aakash\Desktop\sample.xml");
XmlTextWriter x = null;
MemoryStream ms = null;
ms = new MemoryStream();
x = new XmlTextWriter(ms, Encoding.Unicode);
//Write dataset to writer
ds.WriteXml(x);
//Get the length of stream
int count=(int)ms.Length;
//get an array to read the stream
byte[] arr=new byte[count];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(arr, 0, count);
//Use encoding
UnicodeEncoding utf = new UnicodeEncoding();
return utf.GetString(arr).Trim();
}
No comments:
Post a Comment