How to use XML to bulk write data to SQL Server 2005: About XML Time formats

Source: Internet
Author: User
Tags empty serialization static class xmlns

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); }
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.