1.首先将后台数据转成对应的XML数据格式
/// <summary> /// 集合转XML数据格式 /// </summary> /// <param name="list">集合</param> /// <param name="xelementName">子元素名称</param> /// <returns></returns> public static XElement ConvertToAssetNumberListXml(List<string> list,string xelementName) { var xml = new XElement("root"); var items = list.Select(n => new XElement(xelementName, n)).ToList(); if (items.Count > 0) { xml.Add(items); } return xml; }
List<string> list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("3");
list.Add("4");
string a= ConvertToAssetNumberListXml(ages,"name").ToString();//注意转成XML格式后是需要ToString()
数据格式样例
//<root>
//<name>1</name>
//<name>2</name>
//<name>3</name>
//<name>4</name>
//</root>
2.指定传入存储的参数格式为Xml
var param = new DynamicParameters(); param.Add("@listXml", querySearch.ListXml.ToString(), DbType.Xml);
3.数据库模拟测试xml数据存入表(此处是存入临时表)
if object_id('tempdb..#tempAssetNumberList') is not null Begin drop table #tempAssetNumberList End declare @listXml xml set @listXml= '<root> <name>1</name> <name>2</name> <name>3</name> <name>4</name> </root>' select T.c.value('.[1]','nvarchar(16)') as name into #tempAssetNumberList --此name决定的就是字段名称 from @listXml.nodes('/root/name') as T(c) select * from #tempAssetNumberList