Link to home
Start Free TrialLog in
Avatar of andrepires
andrepiresFlag for Brazil

asked on

Saving data to xml fields with C# and SQL Server 2005 Database

Hello Experts!
Suppose I have a table in my sql server 2005 database that contains an xml field (using the xml data type).
Now suppose that I use the following code to read data from that field:

protected bool LoadXml(SqlConnection cn, XmlDocument doc)
{
        //Reading the xml from the database
        string sql =  @"SELECT Id, XmlField  FROM TABLE_WITH_XML_FIELD WHERE Id = @Id";
        SqlCommand cm = new SqlCommand(sql, cn);
        cm.Parameters.Add(new SqlParameter("@Id",1));
        using (SqlDataReader dr = cm.ExecuteReader())
        {
                 if (dr.Read())
                {
                          SqlXml MyXml= dr.GetSqlXml(dr.GetOrdinal("XmlField"));
                          doc.LoadXml( MyXml.Value);
                          return true;
                }
                else
                {
                          return false;
                }
         }
 }

Ok... I can read it.
My question is: How can I save data to that xml field?

Regards,

Andre
ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andrepires

ASKER

Thank you!