如何用c#写方法把xml文件读到数据库中

如何用c#写方法把xml文件读到数据库中,第1张

using System

using System.Text

using System.Xml

namespace ParsingXml

{

class Program

{

static void Main(string[] args)

{

XmlReader xmlReader = XmlReader.Create("c:/eurofxref-daily.xml")

while(xmlReader.Read())

{

if((xmlReader.NodeType == XmlNodeType.Element) &&(xmlReader.Name == "Cube"))

{

if(xmlReader.HasAttributes)

Console.WriteLine(xmlReader.GetAttribute("currency") + ": " + xmlReader.GetAttribute("rate"))

}

}

Console.ReadKey()

}

}

}

sqlserver2005分解并导入xml文件[@more@]

1.

一次性导入:

declare

@idoc

int

declare

@doc

xml

select

@doc=bulkcolumn

from

openrowset(bulk

n'e:mstarindustrycodes.xml',

single_blob)

as

x

exec

sp_xml_preparedocument

@idoc

output,

@doc

select

*

into

tmp_tab

from

openxml

(@idoc,

'/root/record'/',2)

with

(

industrycode

varchar(10)

,industryglobalid

varchar(10)

,industryname

varchar(100)

,sectorcode

varchar(10)

,sectorglobalid

varchar(10)

,sectorname

varchar(100)

,supersectorcode

varchar(10)

,supersectorname

varchar(100)

,groupcode

varchar(10)

,groupname

varchar(100)

,countryid

varchar(3)

)

exec

sp_xml_removedocument

@idoc

select

*

from

tmp_tab

2.

先导入到表中varchar(max)列,然后再用openxml解析,读出。

--

使用single_clob参数,tmp_raw中字段为varcahr(max)类型

select

*

into

tmp_raw

from

openrowset(bulk

n'e:mstarindustrycodes.xml',

single_clob)

as

x

declare

@idoc

int

declare

@doc

xml

select

@doc

=

bulkcolumn

from

tmp_raw

exec

sp_xml_preparedocument

@idoc

output,

@doc

select

top

10

*

from

openxml

(@idoc,

'/root/record',

1)

with

(

industrycode

varchar(10)

,industryglobalid

varchar(10)

,industryname

varchar(100)

,sectorcode

varchar(10)

,sectorglobalid

varchar(10)

,sectorname

varchar(100)

,supersectorcode

varchar(10)

,supersectorname

varchar(100)

,groupcode

varchar(10)

,groupname

varchar(100)

,countryid

varchar(3)

)

exec

sp_xml_removedocument

@idoc


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6697295.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存