您的位置:首页 > 数据库

序列化XML的类。包括向SQL传XML数据

2016-07-04 15:28 393 查看
向存储过程传XML数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Xml;
using System.Xml.Serialization;

namespace JHDWCF.Common
{

/// <summary>
/// Xml序列化与反序列化
/// </summary>
public class XmlUtil
{
#region 反序列化
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="xml">XML字符串</param>
/// <returns></returns>
public static object Deserialize(Type type, string xml)
{
try
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(sr);
}
}
catch (Exception e)
{

return null;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type"></param>
/// <param name="xml"></param>
/// <returns></returns>
public static object Deserialize(Type type, Stream stream)
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(stream);
}
#endregion

#region 序列化
/// <summary>
/// 序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string Serializer(Type type, object obj)
{
MemoryStream Stream = new MemoryStream();
XmlSerializer xml = new XmlSerializer(type);
try
{
//序列化对象
xml.Serialize(Stream, obj);
}
catch (InvalidOperationException)
{
throw;
}
Stream.Position = 0;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd();

sr.Dispose();
Stream.Dispose();

return str;
}

/// <summary>
/// 序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string SerializerForSQL(Type type, object obj)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "    ";
settings.NewLineChars = "\r\n";
settings.Encoding = Encoding.UTF8;
settings.OmitXmlDeclaration = true;  // 不生成声明头
MemoryStream Stream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(obj.GetType());
using (XmlWriter xmlWriter = XmlWriter.Create(Stream, settings))
{
// 强制指定命名空间,覆盖默认的命名空间
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(xmlWriter, obj, namespaces);
xmlWriter.Close();
};

StreamReader sr = new StreamReader(Stream);
Stream.Position = 0;
string str = sr.ReadToEnd();

sr.Dispose();
Stream.Dispose();

return str;
}

#endregion
}
}


//数据库调用方式如下 

DECLARE @x xml ;

SET @x='<ArrayOfInt>

  <int>1</int>

  <int>2</int>

  <int>3</int>

  <int>4</int>

  <int>56</int>

</ArrayOfInt>';

DECLARE @tb TABLE(id int)

DECLARE @id INT;

EXEC sp_xml_preparedocument @id output,@x;

insert @tb(id) select id  from openxml(@id, '/ArrayOfInt/int',1)

with (id int 'text()')

EXEC SP_XML_REMOVEDOCUMENT @id;

select * from @tb;

增加多个字段类型的sql

DECLARE @x xml ;

SET @x='<ArrayOfInt>

<model>

  <id>1</id>

  <name>水</name>

  <price>3.01</price>

</model>

<model>

  <id>2</id>

  <name>茶</name>

  <price>8.51</price>

</model>

</ArrayOfInt>';

DECLARE @tb TABLE(id INT,NAME NVARCHAR(20),Price DECIMAL(18,2));

DECLARE @id INT;

EXEC sp_xml_preparedocument @id output,@x;

insert @tb(id,NAME,price) select id,NAME,Price  from openxml(@id, '/ArrayOfInt/model',1)

with (id int 'id/text()',name NVARCHAR(20) 'name/text()',price DECIMAL(18,2) 'price/text()')

EXEC SP_XML_REMOVEDOCUMENT @id;

具体的细节参考http://blog.sina.com.cn/s/blog_a5193ed401017p35.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: