[ Team LiB ] |
Recipe 8. Storing XML to a Database FieldProblemYou need to store XML to a field in a database. SolutionStore the contents of the InnerXml of the XmlDocument to the database. You can later load this into an empty XmlDocument with LoadXml( ). The schema of table TBL0804 used in this solution is shown in Table 8-4.
The sample code contains five event handlers:
The C# code is shown in Example 8-7. Example 8-7. File: StoreXmlFieldForm.cs// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.IO; using System.Xml; using System.Data; using System.Data.SqlClient; private DataTable dt; private SqlDataAdapter da; private const String TABLENAME = "TBL0804"; // . . . private void StoreXmlFieldForm_Load(object sender, System.EventArgs e) { String selectText = "SELECT Id, XmlField FROM " + TABLENAME; String insertText = "INSERT " + TABLENAME + " (Id, XmlField) " + "VALUES (@Id, @XmlField)"; String updateText = "UPDATE " + TABLENAME + " " + "SET XmlField = @XmlField " + "WHERE Id = @Id"; // Create the data adapter. da = new SqlDataAdapter(selectText, ConfigurationSettings.AppSettings["Sql_ConnectString"]); da.UpdateCommand = new SqlCommand(updateText, da.SelectCommand.Connection); da.UpdateCommand.CommandType = CommandType.Text; da.UpdateCommand.Parameters.Add("@Id", SqlDbType.Int, 0, "Id"); da.UpdateCommand.Parameters.Add("@XmlField", SqlDbType.NText, 0, "XmlField"); da.InsertCommand = new SqlCommand(insertText, da.SelectCommand.Connection); da.InsertCommand.CommandType = CommandType.Text; da.InsertCommand.Parameters.Add("@Id", SqlDbType.Int, 0, "Id"); da.InsertCommand.Parameters.Add("@XmlField", SqlDbType.NText, 0, "XmlField"); dt = new DataTable( ); da.FillSchema(dt, SchemaType.Source); da.Fill(dt); } private void writeButton_Click(object sender, System.EventArgs e) { // Load the ID into variable and text box into XmlDoc. int id = 0; XmlDocument xmlDoc = new XmlDocument( ); try { id = Int32.Parse(idTextBox.Text); xmlDoc.LoadXml(xmlTextBox.Text); } catch(Exception ex) { MessageBox.Show("ERROR: " + ex.Message); return; } // Find the row with the ID entered. DataRow row = dt.Rows.Find(new object[] {id}); if(row != null) // For an existing row, update the XmlField. row["XmlField"] = xmlDoc.InnerXml; else // For a new row, add the row with the ID and XmlField. dt.Rows.Add(new object[] {id, xmlDoc.InnerXml}); // Update the database using the DataAdapter. da.Update(dt); } private void readButton_Click(object sender, System.EventArgs e) { // Load the ID into variable from text box. int id = 0; try { id = Int32.Parse(idTextBox.Text); } catch(Exception ex) { MessageBox.Show("ERROR: " + ex.Message); return; } // Find the row with the ID entered. DataRow row = dt.Rows.Find(new object[] {id}); if(row != null) { // If found, load the XML column value from the row. XmlDocument xmlDoc = new XmlDocument( ); xmlDoc.LoadXml(row["XmlField"].ToString( )); // Display the XML. xmlTextBox.Text = xmlDoc.InnerXml; } else xmlTextBox.Text = "Record not found for Id = " + id; } private void sampleXmlButton_Click(object sender, System.EventArgs e) { DataSet ds = new DataSet( ); // Fill the Categories table and add it to the DataSet. SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 3 * FROM Orders", ConfigurationSettings.AppSettings["Sql_ConnectString"]); da.FillSchema(ds, SchemaType.Source, "Orders"); da.Fill(ds, "Orders"); // Write the XML for the DataSet to a StringWriter, and output. StringWriter sw = new StringWriter( ); ds.WriteXml(sw, XmlWriteMode.WriteSchema); xmlTextBox.Text = sw.ToString( ); ds.Dispose( ); idTextBox.Clear( ); } private void clearButton_Click(object sender, System.EventArgs e) { idTextBox.Clear( ); xmlTextBox.Clear( ); } DiscussionThe solution demonstrates how to store XML data in a text field of a database table and subsequently read it into an XmlDocument using the LoadXml( ) method. Standard database access techniques using a DataAdapter and DataTable are used. |
[ Team LiB ] |