namespace Ionic.Demo
{
public class DataSetCreator
{
public static void Main(string[] args) {
try {
DataSetCreator me= new DataSetCreator();
me.CreateDataSetAndSave();
System.Console.WriteLine("exiting...");
}
catch (System.Exception e) {
System.Console.WriteLine("Exception...\n" + e);
}
}
public void CreateDataSetAndSave() {
System.Random rnd = new System.Random();
System.Data.DataSet ds1= new System.Data.DataSet();
System.Data.DataRow row;
// get a stream associated to the string (could alternatively get a
stream by opening a file)
System.IO.MemoryStream ms = StringToMemoryStream(TheXsd);
System.IO.StreamReader myStreamReader = new
System.IO.StreamReader(ms);
// read in the schema
ds1.ReadXmlSchema(myStreamReader);
System.Data.DataTable categoriesT = ds1.Tables["Category"]; // this
is one of the tables defined in the XSD
string[] categoryList = new string[] {"animal", "vegetable",
"mineral", "other"};
int i;
for (i=0; i < categoryList.Length; i++) {
row= categoriesT.NewRow();
row["name"]= categoryList[i];
row["id"]= i;
categoriesT.Rows.Add(row);
}
System.Data.DataTable thingsT= ds1.Tables["Thing"]; // another table
from the XSD
string[] things = new string[] {"pencil", "tomato", "armadillo",
"spaceship"};
for (i=0; i < things.Length; i++) {
row= thingsT.NewRow();
row["Name"]= things[i];
row["categoryId"]= rnd.Next(categoryList.Length);
thingsT.Rows.Add(row);
}
ds1.WriteXml(System.Console.Out);
System.Console.WriteLine("\n");
// could alternatively save the output (XML Dataset) to a file:
//System.IO.Stream stream = System.IO.File.Open(dirPath +
"\\dataset-filename.xml", System.IO.FileMode.Create);
//ds1.WriteXml(stream);
//stream.Close();
}
// turn a string into a stream:
static System.IO.MemoryStream StringToMemoryStream(string s) {
System.Text.ASCIIEncoding enc= new System.Text.ASCIIEncoding();
int byteCount = enc.GetByteCount(s.ToCharArray(), 0, s.Length);
byte[] ByteArray=new byte[byteCount];
int bytesEncodedCount = enc.GetBytes(s, 0, s.Length, ByteArray, 0);
System.IO.MemoryStream ms = new System.IO.MemoryStream(ByteArray);
return ms;
}
public static string TheXsd=
"<?xml version='1.0' encoding='utf-8' ?>" +
"<xs:schema id='CategoriesSchema' xmlns=''
xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'>" +
" <xs:element name='CategoriesSchema' msdata:IsDataSet='true'>" +
" <xs:complexType>" +
" <xs:choice maxOccurs='unbounded'>" +
" <xs:element name='Category'>" +
" <xs:complexType>" +
" <xs:attribute name='name'
type='xs:string' />" +
" <xs:attribute name='id'
form='unqualified' type='xs:integer' />" +
" </xs:complexType>" +
" </xs:element>" +
" <xs:element name='Thing'>" +
" <xs:complexType>" +
" <xs:sequence>" +
" <xs:element
name='Name' type='xs:string' minOccurs='0' msdata:Ordinal='0' />" +
" <xs:element
name='categoryId' type='xs:integer' minOccurs='0' msdata:Ordinal='0' />" +
" </xs:sequence>" +
" </xs:complexType>" +
" </xs:element>" +
" </xs:choice>" +
" </xs:complexType>" +
" <xs:key name='key1'>" +
" <xs:selector xpath='.//Category' />" +
" <xs:field xpath='@id' />" +
" </xs:key>" +
" <xs:keyref name='NoteCategory' refer='key1'>" +
" <xs:selector xpath='.//Note' />" +
" <xs:field xpath='categoryId' />" +
" </xs:keyref>" +
" </xs:element>" +
"</xs:schema>" ;
}
}
"Chris Kennedy" <ch**********@cybase.co.uk> wrote in message
news:uj**************@TK2MSFTNGP09.phx.gbl...
How do create a dataset from scratch based on an XML schema. This will not
be filled by a dataadapter. It will be a dataset which I add rows to and
then save as an XML file. All the example depend on filling the dataset. I
have also tried creating it from the XML schema but I am having problems
manipulating it. If someone could point me to some resources, that would
be great as I can't find anything.