Creating xml document from sql xml fieldhttp://forums.asp.net/t/1485944.aspx/1?Creating+xml+document+from+sql+xml+fieldMon, 16 Apr 2012 14:57:13 -040014859443479796http://forums.asp.net/p/1485944/3479796.aspx/1?Creating+xml+document+from+sql+xml+fieldCreating xml document from sql xml field <p>Hi,</p> <p>I have this code that writes&nbsp;xml content to database field (Type:XML, not nvarchar or ntext):</p> <pre class="prettyprint">reader = new XmlTextReader(fi.FullName); reader.WhitespaceHandling = WhitespaceHandling.None; xmlDoc = new XmlDocument(); xmlDoc.Load(reader); reader.Close(); param = new SqlParameter(&quot;@XML&quot;, System.Data.SqlDbType.Xml); param.Value = new SqlXml(new XmlTextReader(xmlDoc.InnerXml.ToString().Trim(), XmlNodeType.Document, null)); cmd.Parameters.Add(param); //cmd.Parameters.AddWithValue(&quot;@XML&quot;, xmlDoc.InnerXml.ToString().Trim()); SqlParameter output = new SqlParameter(&quot;@Err_Description&quot;, System.Data.SqlDbType.VarChar, 100); output.Direction = System.Data.ParameterDirection.Output; cmd.Parameters.Add(output); dr = cmd.ExecuteReader(); err = cmd.Parameters[&quot;@Err_Description&quot;].Value.ToString();</pre> <P><BR>&nbsp;If I check "xmlDoc.InnerXml" value I can see that the header line :<BR>&lt;?xml version="1.0" encoding=.... is part of the text. but after writing to database, the field doesnt contain this header line!</P> <P>I tried using nvarchar or ntext insted of XML type for that field but then the above code stoped working! (exception).</P> <P>I need to write this header line back to xml file through this code:</P><pre class="prettyprint">string sql = "Select [XML] " + "From Tbl_Rolls " + "Where BarcodeUID='" + barcode + "' AND " + "[XML] IS NOT NULL"; cmd = new SqlCommand(sql, conn); //dr = cmd.ExecuteReader(); //dr.Read(); XmlReader rdr = cmd.ExecuteXmlReader(); /*SqlXml xml = dr.GetSqlXml(0); new XmlTextWriter(ConfigurationManager.AppSettings["RePrintFolder"].ToString() + barcode + ".xml", System.Text.Encoding.Unicode).WriteNode( xml.CreateReader(), true );*/ new XmlTextWriter(ConfigurationManager.AppSettings["RePrintFolder"].ToString() + barcode + ".xml", System.Text.Encoding.GetEncoding("WINDOWS-1255")).WriteNode(rdr, true);</pre> <p><br> How can I solve it?</p> <p>Thanks in advance.</p> 2009-10-27T15:33:43-04:003488576http://forums.asp.net/p/1485944/3488576.aspx/1?Re+Creating+xml+document+from+sql+xml+fieldRe: Creating xml document from sql xml field <p>Hi moodi_z,<br> </p> <p>In SQL Server, user-provided encoding is not preserved. I think you can convert the XML to a string type and then add the XML declaration. <br> <br> Please refer to another similar thread for more information:</p> <p><a href="/t/1455808.aspx">http://forums.asp.net/t/1455808.aspx</a><br> </p> 2009-11-02T05:19:32-05:004934982http://forums.asp.net/p/1485944/4934982.aspx/1?Re+Creating+xml+document+from+sql+xml+fieldRe: Creating xml document from sql xml field <p>I know this is old, but I swear it's like you never read the question. I'm having a similar issue and this doesn't even remotely begin to deal with what I perceive is a simple issue to reeolve that I haven't figured out yet. For me reading this result of my search was a waste of time.</p> 2012-04-16T14:57:13-04:00