You often encounter situations where you need to insert bulk data into SQL Server and then further process the data in a stored procedure. The stored procedure does not have a parameter type such as an array or a list, and the XML type solves the problem properly.
However, SQL Server2005 support for standard XML is inadequate and many areas require special processing. Give an example to illustrate.
This scenario is a list<model> that passes an XML serialization to the stored procedure.
The code for 1.Model is as follows, which is an entity class
public class Model
{
///<summary>
///uin
///</summary>
[XmlElement ("UIn")]
Public long UIn {get; set;}
<summary>
///Nickname
///</summary>
[XmlElement (' Name ')] public
string Name {get; set;}
///<summary>
///avatar
///</summary>
[XmlElement ("IMG")] public
string IMG {get ; Set
///<summary>
///access time
///</summary>
[XmlElement ("Visittime")]
public DateTime visittime {get; set;}
}
Then we need to serialize this list<model> into an XML string. However, SQL Server has a problem with namespace recognition of XML, and. NET default serialization appears xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=http:/ /www.w3.org/2001/xmlschema
A netizen has given a class (reference http://www.cnblogs.com/prime/archive/2012/10/11/SQLXML.html) for a perfectly serialized SQL SERVER2005 supported XML:
public static class Dbxml {private static readonly xmlserializernamespaces namespaces = new XmlSerializerNamespaces (
); Static Dbxml () {//Remove xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= http://www.w3.org/2001/ XmlSchema "Namespaces.add (string. Empty, String.
Empty); ///<summary>///Serializes an object into an XML string///</summary>///<typeparam name= "T" ></typepa ram>///<param name= "obj" ></param>///<returns></returns> public static string Se
rializexml<t> (t obj) {XmlSerializer serializer = new XmlSerializer (typeof (T)); using (MemoryStream stream = new MemoryStream ()) {serializer.
Serialize (stream, obj, namespaces); Return Encoding.UTF8.GetString (stream.
ToArray ()); The public static T deserializexml<t> (String obj) {XmlSerializer serializer = new Xmlseria Lizer (typeof (T));
using (StringReader reader = new StringReader (obj)) {return (T) serializer.
Deserialize (reader); }
}
}