Here is some code I use to load the data into a dataset:
XmlTextReader xmlReader = new XmlTextReader(stream);
XmlDocument _doc = new XmlDocument();
XslTransform _transform = new XslTransform();
_doc.Load(xmlReader);
DataSet ds = new DataSet();
ds.ReadXml(stream, XmlReadMode.InferSchema);
ds.Namespace = "#RowsetSchema";
When I use ds.GetXml(); I get the following:
<xml xmlns="#RowsetSchema">
<rs:data xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" ContactName="Maria Anders" ContactTitle="Sales Representative" Address="Obere Str. 57" City="Berlin" PostalCode="12209" Country="Germany" Phone="030-0074321" Fax="030-0076545" xmlns:z="#RowsetSchema" />
<z:row CustomerID="ANATR" CompanyName="Ana Trujillo Emparedados y helados" ContactName="Ana Trujillo" ContactTitle="Owner" Address="Avda. de la Constitución 2222" City="México D.F." PostalCode="05021" Country="Mexico" Phone="(5) 555-4729" Fax="(5) 555-3745" xmlns:z="#RowsetSchema" />
</rs:data>
</xml>
Everything is great and all but I can't seem to access the columns. I would like to format some column names (i.e. "ContactName" to "Contact Name"). Also I would like to append or format some values for example change every instance PostalCode to include an additional "-0000" at the end.
Subject:Accessing a Dataset Author:Ivan Pedruzzi Date:24 Mar 2005 12:02 AM
Yamazed,
Out of curiosity is the stream object pointing to an XML file?
If yes why are you using a DataSet and not just load the file into an XmlDocument object?
XML element name and attribute name can't contain space.
Subject:Accessing a Dataset Author:Yama Kamyar Date:24 Mar 2005 12:31 PM
Hi,
For those of you interested here is how I accessed the loaded dataset using ds.ReadXml(stream, XmlReadMode.InferSchema).
/// <summary>
/// Format Dataset:
/// Format Column names where necessary
/// <remark>
/// could retrieve from a config file then loop
/// through and change column names
/// </remark>
/// Convert, parse, or map the row to appropriate values
/// </summary>
/// <param name="ds">
/// ds.ReadXml(stream, XmlReadMode.InferSchema)
/// stream = well formed XML data in MemoryStream
/// </param>
private void PrintValues(DataSet ds)
{
Debug.Write("\r\n" + "TableName: ");
foreach(DataTable dt in ds.Tables)
{
Debug.Write("TableName: " + dt.TableName);
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dt.Columns)
{
switch(dc.ColumnName)
{
case "ColumnName1":
dc.ColumnName = "Column Name 1";
break;
case "ColumnName2":
dc.ColumnName = "Column Name 2";
Convert.ToInt64(dr[dc]));
break;
// etc...
}
Debug.Write("\t " + dc.ColumnName + ": " + dr[dc]);
}
Debug.WriteLine("");
ds.AcceptChanges();
}
}
}