I am simply trying to call a store procedure (SQL Server 2008) using C# and passing XMLDocument to a store procedure parameter that takes a SqlDbType.Xml data type. I am ge
you can add parameter in more simpler way in this way we don't have to pass object type to parameter sql manages it as passed value
SqlXml sqlXml = new SqlXml(xmlReader);
cmd.Parameters.AddWithValue("@FileContent"], strxml);
Or, with the fewest lines of code, read your XmlDocument straight into an XmlNodeReader and use that to initialise you SqlXml parameter value:
SqlXml sqlXml= new SqlXml(new XmlNodeReader(doc));
cmd.Parameters.Add("@FileContent", sqlXml);
Note that you don't need to add the parameter with the type, then set the value - if you pass a type which SqlParameter recognises (in this case a SqlXml object), the type will be inferred.
In .NET Framework 4.5.2, I was able to pass a System.Xml.XmlDocument (variable name "xdoc") object using the following simple code:
XmlTextReader xreader = new XmlTextReader(new StringReader(xdoc.OuterXml));
cmd.Parameters.Add(new SqlParameter("@xmlOptions", new SqlXml(xreader)));