473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Creating XML DataSet from scratch

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.
Nov 12 '05 #1
1 3297

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.

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric Caron | last post by:
Hi I try to import file in a dataset. How I can do this ?
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
1
by: Marco Martin | last post by:
Hi everyone, I've got a dataset with a records table that is built up from scratch according to user input. I'm saving this dataset as xml by using...
0
by: Thaddeus | last post by:
//Author: //Thaddeus Jacobs, MCP //Kinematic Automation, Inc. //mailto:tjacobs@kinematic.com // //Description: //convert ADO .NET dataset to ADO 2.5 2.6 2.7 recordset and v/v //DataSet to...
1
by: Arpan | last post by:
The following ASPX code snippet creates a DataSet programmatically right from the scratch: 'create an empty DataSet Dim objDS As New DataSet("MyDataSet") 'create a new table & add columns Dim...
2
by: Gary Shell | last post by:
I have jumped in to the deep end of the pool, trying out visual inheritance of a form and have run into a snag. I have the need to create a simple maintenance form for five identically...
2
by: JohnR | last post by:
I have a table in a dataset whose fields are bound to various controls on my form. The records are sorted by primary key so when I use the currencymanager to navigate the data, it all appears...
2
by: DC | last post by:
Hi, I can easily create an xsd from an xml file in Visual Studio 2005. But I want a typed DataSet, so is there an option to create that typed DataSet from the xsd or the xml file? I need many...
1
by: RBScheer | last post by:
Hi. I have written unit tests for my helper classes that return simple values as strings or integers. Now I need to test the methods that deal with DataSets and DataTables. Is there any special...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.