[quoted text, click to view] "Thomas Lischetzki" <thomas.lischetzki@nospam.com> wrote in message news:emvhJskGFHA.2936@TK2MSFTNGP15.phx.gbl...
> I have a little problem importing the following xml file into a dataset.
: :
> That's fine, but how can I get access to the information within my code?
> Hope you understand my problem and can help me.
Thomas,
My understanding is that you can access the DataSet inferred from this
XML document in Visual Studio .NET, but are looking for the code that
prepares a DataSet to load this XML document programmatically.
Here's the C# for creating a DataSet with a schema consistent with that
of your original XML. There is some variation in the type of DataSet
you need to accomodate the data, so you may have to tune this to
what your specific requirements are. In particular, I chose String as
the type for several columns where DateTime (for the 'time' attribute)
or Int32 (for the text child nodes, SimpleContent, of the 'data' tags)
may be more appropriate.
- - - LogfileDataSet.cs (excerpt)
using System;
using System.Data;
using System.Xml;
// . . .
DataColumn dc;
DataSet dataSet1 = new DataSet("DataSet1");
dataSet1.EnforceConstraints = true;
DataTable logfile = new DataTable("logfile");
logfile.Columns.Add(new DataColumn("version", typeof(string), null, MappingType.Attribute));
logfile.Columns.Add(new DataColumn("logfile_Id", typeof(int), null, MappingType.Hidden));
dc = logfile.Columns["logfile_Id"];
logfile.Constraints.Add(new UniqueConstraint(
"Constraint1",
new DataColumn[] { dc },
true
));
dc.AutoIncrement = true;
dc.AllowDBNull = false;
dc.Unique = true;
dc = logfile.Columns["version"];
dc.AllowDBNull = false;
dataSet1.Tables.Add(logfile);
DataTable log = new DataTable("log");
dc = new DataColumn("date", typeof(string), null, MappingType.Attribute);
dc.AllowDBNull = false;
log.Columns.Add(dc);
dc = new DataColumn("time", typeof(string), null, MappingType.Attribute);
dc.AllowDBNull = false;
log.Columns.Add(dc);
dc = new DataColumn("username", typeof(string), null, MappingType.Attribute);
dc.AllowDBNull = false;
log.Columns.Add(dc);
dc = new DataColumn("level", typeof(string), null, MappingType.Attribute);
dc.AllowDBNull = false;
log.Columns.Add(dc);
dc = new DataColumn("message", typeof(byte), null, MappingType.Attribute);
dc.AllowDBNull = false;
log.Columns.Add(dc);
dc = new DataColumn("log_Id", typeof(int), null, MappingType.Hidden);
log.Columns.Add(dc);
log.Constraints.Add(
new UniqueConstraint(
"Constraint1",
new DataColumn[] { dc },
true
));
dc.AutoIncrement = true;
dc.AllowDBNull = false;
dc.Unique = true;
dc = new DataColumn("logfile_Id", typeof(int), null, MappingType.Hidden);
log.Columns.Add(dc);
dataSet1.Tables.Add(log);
DataTable data = new DataTable("data");
dc = new DataColumn("order", typeof(byte), null, MappingType.Attribute);
dc.AllowDBNull = false;
data.Columns.Add(dc);
dc = new DataColumn("caption", typeof(string), null, MappingType.Attribute);
dc.AllowDBNull = false;
data.Columns.Add(dc);
dc = new DataColumn("text", typeof(string), null, MappingType.SimpleContent);
dc.AllowDBNull = false;
data.Columns.Add(dc);
dc = new DataColumn("log_Id", typeof(int), null, MappingType.Hidden);
data.Columns.Add(dc);
dataSet1.Tables.Add(data);
ForeignKeyConstraint fkc;
fkc = new ForeignKeyConstraint(
"logfile_log",
new DataColumn[] { logfile.Columns["logfile_Id"] },
new DataColumn[] { log.Columns["logfile_Id"] }
);
fkc.AcceptRejectRule = AcceptRejectRule.None;
fkc.DeleteRule = Rule.Cascade;
fkc.UpdateRule = Rule.Cascade;
log.Constraints.Add(fkc);
fkc = new ForeignKeyConstraint(
"log_data",
new DataColumn[] { log.Columns["log_Id"] },
new DataColumn[] { data.Columns["log_Id"] }
);
fkc.AcceptRejectRule = AcceptRejectRule.None;
fkc.DeleteRule = Rule.Cascade;
fkc.UpdateRule = Rule.Cascade;
data.Constraints.Add(fkc);
DataRelation logfile_log = new DataRelation(
"logfile_log",
logfile.Columns["logfile_Id"],
log.Columns["logfile_Id"],
false
);
logfile_log.Nested = true;
dataSet1.Relations.Add(logfile_log);
DataRelation log_data = new DataRelation(
"log_data",
log.Columns["log_Id"],
data.Columns["log_Id"],
false
);
log_data.Nested = true;
dataSet1.Relations.Add(log_data);
// . . .
- - -
How'd I get this? :-) You need to infer an XML Schema document from
your XML instance document (the inferred schema is one possible schema
that fits your instance document; this is also a stage at which you may want
to tweak but recognize that the inference engine makes it's decisions with
the next step as it's goal in mind.) Then choose "Generate Dataset" from
the XML Schema in Visual Studio .NET. This will create a DataSet1.cs
source file and add it to your project (among other things, like *.xsx files)
that implements a Typed DataSet corresponding to your XML instance.
The code above is a simplification of that Typed DataSet. If it didn't
immediately strike you as being a Typed DataSet, that's just because
you're seeing the essential preparations to the DataSet's structure that
are necessary. If you skim through the fields, constructors, and Init*
methods of the code xsd.exe will generate automatically, you'll see
very much the same implementation between the event handlers
and extended properties.
Derek Harmon