|
Here's a simplified example. It creates an Xml file named "XmlRoundTrip.xml" on the Desktop from a DataSet and then Reads it back in. Just add the code below to the constructor of a default Form template and run it.
The first Message box shows "Type written: System.Drawing.Point". The dataset is then cleared and the data is re-read. But the second Message box shows "Type read: System.String".
Any ideas? Am I expecting too much from the DS XML methods?
DataSet ds = new DataSet("MyDataSet");<br />
<br />
DataTable dt = ds.Tables.Add("MyDataTable");<br />
<br />
DataColumn dcA = new DataColumn("PointColumn", typeof(Point));<br />
dcA.DefaultValue = new Point(8, 13);<br />
<br />
DataColumn dcB = new DataColumn("SizeColumn", typeof(Size));<br />
dcB.DefaultValue = new Size(21, 34);<br />
<br />
dt.Columns.Add(dcA);<br />
dt.Columns.Add(dcB);<br />
<br />
DataRow dr = dt.NewRow();<br />
dt.Rows.Add(dr);<br />
<br />
string myTempFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);<br />
string myXmlFilename = myTempFolder + "\\XmlRoundTrip.xml";<br />
<br />
ds.WriteXml(myXmlFilename, XmlWriteMode.WriteSchema);<br />
<br />
MessageBox.Show("Type written: " + dt.Rows[0]["PointColumn"].GetType().ToString());<br />
<br />
dt.Clear();<br />
<br />
ds.ReadXml(myXmlFilename, XmlReadMode.ReadSchema);<br />
<br />
MessageBox.Show("Type read: " + dt.Rows[0]["PointColumn"].GetType().ToString());
-- James --
|
|
|
|
|
Good point - the XML generated is bursting with type information, so there's no reason I can see for this not to work, but it doesn't. How pathetic.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
I have a sample coding using C#.NET in Compact Framework. This code is about adding rows and columns in DataTable. Now, I want to save the content of the Datagrid into my XML file. How can I do this?
Then, I would like to retrive the content from the XML file into my Datagrid. How can I load it?
=====Code Start=====
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.XML;
using System.Windows.Forms.DataGrid;
class DataGridAdding : Form
{
private System.Data.DataTable dataTable;
private System.Windows.Forms.Button buttonAddColumn;
private System.Windows.Forms.Button buttonAddRow;
private System.Windows.Forms.Button buttonDeleteColumn;
private System.Windows.Forms.Button buttonDeleteRow;
private int maxColumnNumber;
private System.Windows.Forms.DataGrid dataGrid;
private int maxRowNumber;
public DataGridAdding()
{
maxColumnNumber = 0;
maxRowNumber = 0;
InitializeComponent();
// Create a DataTable of data to use.
InitializeTable();
// Bind the DataGrid to the DataTable.
dataGrid.DataSource = dataTable;
}
protected void InitializeComponent()
{
this.buttonAddColumn = new System.Windows.Forms.Button();
this.buttonAddRow = new System.Windows.Forms.Button();
this.buttonDeleteColumn = new System.Windows.Forms.Button();
this.buttonDeleteRow = new System.Windows.Forms.Button();
this.dataGrid = new System.Windows.Forms.DataGrid();
//
// buttonAddColumn
//
this.buttonAddColumn.Location = new System.Drawing.Point(5, 5);
this.buttonAddColumn.Size = new System.Drawing.Size(80, 20);
this.buttonAddColumn.Text = "Add Column";
this.buttonAddColumn.Click += new System.EventHandler(this.buttonAddColumn_Click);
//
// buttonAddRow
//
this.buttonAddRow.Location = new System.Drawing.Point(5, 30);
this.buttonAddRow.Size = new System.Drawing.Size(80, 20);
this.buttonAddRow.Text = "Add Row";
this.buttonAddRow.Click += new System.EventHandler(this.buttonAddRow_Click);
//
// buttonDeleteColumn
//
this.buttonDeleteColumn.Location = new System.Drawing.Point(90, 5);
this.buttonDeleteColumn.Size = new System.Drawing.Size(145, 20);
this.buttonDeleteColumn.Text = "Delete Current Column";
this.buttonDeleteColumn.Click += new System.EventHandler(this.buttonDeleteColumn_Click);
//
// buttonDeleteRow
//
this.buttonDeleteRow.Location = new System.Drawing.Point(90, 30);
this.buttonDeleteRow.Size = new System.Drawing.Size(145, 20);
this.buttonDeleteRow.Text = "Delete Current Row";
this.buttonDeleteRow.Click += new System.EventHandler(this.buttonDeleteRow_Click);
//
// dataGrid
//
this.dataGrid.Location = new System.Drawing.Point(0, 56);
this.dataGrid.Size = new System.Drawing.Size(240, 216);
this.dataGrid.Text = "dataGrid";
//
// DataGridAdding
//
this.Controls.Add(this.dataGrid);
this.Controls.Add(this.buttonAddColumn);
this.Controls.Add(this.buttonAddRow);
this.Controls.Add(this.buttonDeleteColumn);
this.Controls.Add(this.buttonDeleteRow);
this.Text = "DataGrid Sample";
}
private void InitializeTable()
{
int index;
// Create a DataTable named "Edit Table".
dataTable = new DataTable("Rows Cols Table");
// Create columns in the DataTable.
AddNewColumn();
AddNewColumn();
AddNewColumn();
// Add 20 rows of data to the DataTable.
for (index = 0; index < 20; index++)
{
AddNewRow();
}
}
private void AddNewColumn()
{
string newColumnName;
int index;
// Increment the max column number.
maxColumnNumber++;
// Construct name for new column.
newColumnName = "C " + maxColumnNumber.ToString();
// Create New Column.
dataTable.Columns.Add(newColumnName, Type.GetType("System.String"));
// Add data to each row in the table for the new column.
for (index = 0; index < dataTable.Rows.Count; index++)
{
dataTable.Rows[index][newColumnName] = "R " + index.ToString() + ", " + newColumnName;
}
}
private void AddNewRow()
{
DataRow dataRow;
int index;
string columnName;
// Increment the maximum row number.
maxRowNumber++;
// Create new row from DataTable.
dataRow = dataTable.NewRow();
// Add data to each column in the new row.
for (index = 0; index < dataTable.Columns.Count; index++)
{
columnName = dataTable.Columns[index].ColumnName;
dataRow[columnName] = "R " + maxRowNumber.ToString() + ", " + columnName;
}
dataTable.Rows.Add(dataRow);
}
private void buttonAddColumn_Click(object o, EventArgs e)
{
AddNewColumn();
}
private void buttonAddRow_Click(object o, EventArgs e)
{
AddNewRow();
}
private void buttonDeleteColumn_Click(object o, EventArgs e)
{
if (dataTable.Columns.Count > 0)
{
dataTable.Columns.RemoveAt(dataGrid.CurrentCell.ColumnNumber);
}
}
private void buttonDeleteRow_Click(object o, EventArgs e)
{
if (dataTable.Rows.Count > 0)
{
dataTable.Rows.RemoveAt(dataGrid.CurrentCell.RowNumber);
}
}
public static void Main(string [] args)
{
Application.Run(new DataGridAdding());
}
}
=====Code End=====
Please help~
1. Save content of Datagrid into XML file.
2. Load content from XML file into Datagrid.
|
|
|
|
|
Foll are the output of the Structure of DataSet after reading it from a Schema file, with the Relations.
Tables count=3
TableName='Calls'.
Columns count=1
ColumnName='Calls_Id', type = System.Int32
TableName='Call'.
Columns count=5
ColumnName='DateTime', type = System.String
ColumnName='TSR_ID', type = System.String
ColumnName='Comments', type = System.String
ColumnName='Call_Id', type = System.Int32
ColumnName='Calls_Id', type = System.Int32
TableName='Evaluation'.
Columns count=4
ColumnName='MonitorName', type = System.String
ColumnName='DateTime', type = System.String
ColumnName='Evaluation_Id', type = System.Int32
ColumnName='Call_Id', type = System.Int32
RelationName='Call_Evaluation
Parent Table= Call
Child Table=Evaluation
Cols=Call_Id, AND Call_Id,
RelationName='Calls_Call
Parent Table= Calls
Child Table=Call
Cols=Calls_Id, AND Calls_Id,
But the problem is in the nesting. Though the relations seem to exist, but I don't think they are actually being populated that way. Here is the output after putting values in the DataSet.
If you notice, the parent ID has not been populated on it's own since the relation has been defined.
TableName='Calls'.
Rows count=1
ColumnName='Calls_Id', value = 0
TableName='Call'.
Rows count=3
ColumnName='DateTime', value = 1
ColumnName='TSR_ID', value = 62137
ColumnName='Comments', value = 1
ColumnName='Call_Id', value = 0
ColumnName='Calls_Id', value =
ColumnName='DateTime', value = 1
ColumnName='TSR_ID', value = 1
ColumnName='Comments', value = 1
ColumnName='Call_Id', value = 1
ColumnName='Calls_Id', value =
ColumnName='DateTime', value = 1
ColumnName='TSR_ID', value = 1
ColumnName='Comments', value = 1
ColumnName='Call_Id', value = 2
ColumnName='Calls_Id', value =
TableName='Evaluation'.
Rows count=3
ColumnName='MonitorName', value = OP NMay
ColumnName='DateTime', value = 2003-09-10
ColumnName='Evaluation_Id', value = 0
ColumnName='Call_Id', value =
ColumnName='MonitorName', value =
ColumnName='DateTime', value =
ColumnName='Evaluation_Id', value = 1
ColumnName='Call_Id', value =
ColumnName='MonitorName', value =
ColumnName='DateTime', value =
ColumnName='Evaluation_Id', value = 2
ColumnName='Call_Id', value =
Am I missing something here ? I really need to solve this problem asap.
Thanks,
Paul
|
|
|
|
|
Hi,
I am populating DataSet from an XML Schema which has tables pointing to other tables.
All the Relations, column names, etc have been successfully retrieved in the DataSet.
But there is problem of nesting. Is there something I have to do to see that tables are nested properly. Like there is a column Call_Id that is added on it's own by the DataSet as it is a child table of Table Call.
Do I have to fill something in that for proper nesting of tables !?!
Any help would be highly appreciated !
Paul
|
|
|
|
|
Hi,
I am deriving a DataSet using ReadXMLSchema method from a XML Schema Definition file.
I want to fill values in the Tables of the Data Set. There are some Tables that are complex Types. How do I fill values in such columns.
For Ex:
Table A
-----Table B (Attributes: 1,2,3,4)
-----Table C (Attributes: Table D, complexType)
---------------Table D (Attributes: 1)
--------------------------Table E ((Attributes: 1,2)
B & C are child of A. D & E are child of C.
C only has one column which is a reference to Table D.
How do I fill the values for the column in Table C , which is Table D (complex type)?
Paul
|
|
|
|
|
Hi all,
Currently I am working on a template solution which transforms contents of a given section (which is in a xml format) with the help of preset XLST files assigned to those sections. The code goes something like this
/Default.aspx
<code>
.
.
.
.
// Get the sections data in XML format
string xmlData = DataLoader.GetSectionData(sectionName, sectionPath, articleId, pageNumber );
// Get that sections template file
string xslFile = Server.MapPath("/")+"Files\\Templates\\"+ templateFile +"\\Default.xsl";
System.IO.StringReader sr = new System.IO.StringReader(xmlData);
XPathDocument doc = new XPathDocument(sr);
XslTransform trans = new XslTransform();
trans.Load(xslFile);
//Transform it and output as HTML
XmlTextWriter writer = new XmlTextWriter(Response.Output);
writer.Formatting = Formatting.None;
trans.Transform(doc,null,writer);
/SectionData.xml
<Root>
<Article>
<Title>Wonders of life</Title>
<Content>
<!--
<u>Sometimes<u>, you wonder how a particular thing went wrong; < b>but most of the time</ b> you wonder how it worked at all
-->
</Content>
</Article></Root>
/SectionTemplate.xslt
.
.
.
<xsl:value-of select="/Root/Article/Title" />
<br />
<xsl:value-of select="/Root/Article/Content/comment()" disable-output-escaping="yes" />
.
.
.
What i expected of the tranformer was to spit the contents of the comment tag to the browser without any parsing/escaping so the html formatting of the given article can be maintained, but whatever is done (I even tried with copy-of xsl element to no effect) the output I get is like this in the article page (HTML VIEW of the generated code)
Wonders of life<br />
<u>Sometimes<u>, you wonder how a particular thing went wrong; <b>but most of the time</b> you wonder how it worked at all
I understand that we can accomplish this by embedding the html code inside CDATA section but that too didnt work out well. Any alternate suggestions are also welcome.
Thanking you
CreProDes
CreProDes
|
|
|
|
|
why are you putting your html in a comment
if you did not put it in a comment then you could use xsl:copy-of
"When the only tool you have is a hammer, a sore thumb you will have."
|
|
|
|
|
Hi,
thank you for the tip. actually previsouly i was having problems using the copy-of tag because the html generated by the client side editor was not perfectly xml compatible one, so i had to go to using the comment and stuff. but now iam making use of SgmlReader from gotdotnet.com and solved that issue as well. Thank you for posting.
Bye,
CreProDes
CreProDes
|
|
|
|
|
I am getting an XML record from a database and I needed to look up a value that it returns from another xml file. e.g.
< PATIENT_DATA>
...
<SERVICE_CODE>123</SERVICE_CODE>
...
</PATIENT_DATA>
i needed to cross reference the service_code field off of another XML file so that the transformed file can display the required SERVICE_CODE into something more meaningful such as "Radiology Services".
I am using an XSL file to format the XML into an HTML. The XML look up file consists of values such as
<root>
...
<key name="123" value="Radiology Services">
...
</root>
My question is how do i achieve this result. Do i have to include the xml file in the XSL file or is there a way to reference another file in the XSL.
Please note that I CANNOT pass more than one file to the xsl compiler. It has to be done in the XSL file.
|
|
|
|
|
|
|
|
I'm using an XML file to store user preferences, but I can't decide on what's the best format. The data would easily been stored in an INI file using sections, keys, and values, but I want to use XML instead. I'm come up with a couple of alternatives:
1.
<?xml version="1.0" encoding="utf-8"?>
<profile>
<section name="Some Section">
<key name="Some Key">Some Value</key>
<key name="Some Other Key">Another Value</key>
</section>
<section name="Some Other Section">
...
</section>
</profile>
2.
<?xml version="1.0" encoding="utf-8"?>
<profile>
<Some_Section>
<Some_Key>Some Value</Some_Key>
<Some_Other_Key>Another Value</Some_Other_Key>
</Some_Section>
<Some_Other_Section>
...
</Some_Other_Section>
</profile>
Which of these two would you choose and why? If you know of a better alternative, I'd love to hear it.
Thanks!
Alvaro
|
|
|
|
|
Out of those two, the first one. Easier to validate, also it's probably easier to enumerate/navigate via XPath, if that were an issue.
Generally, your XML element names should be drawn from a small set of fixed names, which is met by the first example.
HTH
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Yep, I was leaning toward the first one as well, although I originally wrote the code with the second format in mind.
I'm just now barely getting my feet wet with XPath. I used it to retrieve the values for the second format using "section_name/key_name", which seemed pretty simple. For the first format, where attributes are involved, what's the proper XPath string I need? Also if you could provide a link or two that quickly gets me up to speed on the XPath basics, I'd appreciate it very much.
Thanks Stuart!
Regards,
Alvaro
Can I ask you a question?
|
|
|
|
|
To get a particular keys value, you'd use something like '//section[@name='section-name]/key[@name='key-name']/text()' - hmmm, doesn't look any easier, does it. I guess you're probably best going with what you've got - after all, you're not intending to exchange the data with any other application, are you?
The reason I tend to use the first form you presented was because I tend to parse the whole file using libxml2 - the platform I'm limited by doesn't have .NET or an up-to-date MSXML
I'm not sure what the best starters guides to XPath are - I learnt it through learning XSLT from Michael Kay's XSLT reference book (published by Wrox).
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Hi,
I would strongly recommend that you get your basics right with XSL and XPath before jumping into the structure design, as knowing them even at a basic level would give you a clear idea about which is the easiest and efficient way to access and maintain your data.
I liked the following two articles from devshed, through which i learned abt those techs:
XSL - http://www.devshed.com/Server_Side/XML/XSLBasics/XSLBasics1/page1.html
XPath - http://www.devshed.com/Server_Side/XML/XPath/page1.html
All the best.
CreProDes
|
|
|
|
|
the choice depends on what you are trying to do with the xml.
if you want a very generic solution then the first is better.
however, if you want to have a very specific solution the second is better - for example by have different elements for different keys you can set up your xsd to be tighter...
also the second example is easier to understand (from a human point of view)
either way its going to be a {} war... so choose the one your prefer
"When the only tool you have is a hammer, a sore thumb you will have."
|
|
|
|
|
Code along the lines of:
string value1 = currentNode.Attributes["value1"].Value;
However, if the Node does not contain an attribute named "value1", and exception would be thrown.
So, is there a way (without kludging with try..catch blocks) to determine if a given attribute exists for a node?
TIA
~Mike Stanbrook
|
|
|
|
|
IF NOT currentNode.Attributes.ItemOf("value1") IS NOTHING THEN
value1 = currentNode.Attributes.Item("value1")
END IF
|
|
|
|
|
There is an easier way by using XPath Queries.
...asuming you have an XMLDocument Open
Dim xNode As Xml.XmlNode = xDoc.SelectSingleNode("//Element[@Attribute='Value']")
If (Not IsNothing(xNode)) Then
'Do Something
End If
|
|
|
|
|
Are you sure this is easier/more efficient that the method previously suggested, especially given that the current element node is already known and sotred in a local variable?
~Mike Stanbrook
|
|
|
|
|
Hi ,
I want to create one xml document from a dataset where in some fields I am having null values.
can any one help me out?
thanks
|
|
|
|
|
Hey,
If you dont have values, xml generated from the dataset will be having blank values.
Let me know is this your problem ?
Regards
Sri
|
|
|
|
|