简体   繁体   English

从xml文件检索数据并插入数据库表

[英]Retrieve data from xml file and insert into database table

I am using c# .net 1.1 and SQL Server 2000.... 我正在使用c#.net 1.1和SQL Server 2000 ....

I want to retrive data from an xml file and store that data into the database table. 我想从xml文件中检索数据并将该数据存储到数据库表中。

XML file: XML档案:

<information>
  <data>
    <id="1"></id>
    <name>peter</name>
    <age>25</age>
  </data>
</information>

My databse table is 我的数据库表是

id int,
name varchar(100),
age int

First off - your XML is invalid: 首先-您的XML无效:

<data>
    <id="1"></id>

This is not a valid XML construct - what should it be?? 这不是有效的XML构造-应该是什么?

An "ID" attribute on the data node? 数据节点上的“ ID”属性? <data id="1">

An "ID" element with the value inside? 里面有值的“ ID”元素? <data><id>1</id>

There's a ton of ways you can do this - totally depends on your situation: 您可以采取多种方式-完全取决于您的情况:

  • create a XML schema so you can deserialize this XML into a .NET object - works best if you have tons of those files to import 创建一个XML模式,以便可以将该XML反序列化为.NET对象-如果要导入的文件数量很多,则效果最好

  • use Linq-to-XML if you're on .NET 3.5 and up 如果您使用的是.NET 3.5及更高版本,请使用Linq-to-XML

  • use traditional XML document processing 使用传统的XML文档处理

If you use traditional XML document processing, this works best if your files are relatively small (since it loads the entire file into memory). 如果您使用传统的XML文档处理,则在文件较小的情况下(因为它将整个文件加载到内存中),这种方法效果最好。 In that case, you could do something like: 在这种情况下,您可以执行以下操作:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("yourfilename.xml");

XmlNodeList dataNodes = xmlDoc.SelectNodes("/information/data");

foreach(XmlNode node in dataNodes)
{
    int id = Convert.ToInt32(node.SelectSingleNode("id").InnerText);
    string name = node.SelectSingleNode("name").InnerText;
    int age = Convert.ToInt32(node.SelectSingleNode("age").InnerText);

    // insert into database, e.g. using SqlCommand or whatever
}

As for inserting into a database - you can use a stored procedure, or a SQL statement in a SqlCommand - totally up to you. 至于插入数据库-您可以使用存储过程或SqlCommand的SQL语句-完全由您决定。 Once you have the three bits of information (id, name, age), you can do with those whatever you like. 掌握了三部分信息(ID,姓名,年龄)后,您就可以使用自己喜欢的任何信息。

Word of warning also: this includes no error checking whatsoever! 警告词还包括:这不包括任何错误检查 If a <data> node should be incomplete or a sub node has a wrong name, this will crash - you need to provide some error checks, too! 如果<data>节点不完整或子节点名称错误,则会崩溃-您还需要提供一些错误检查! (left out for simplicity) (为简单起见,省去了)

Marc

For when the XML file is visible to the SQL Server service, this works using plain old T-SQL, just tweak it to insert into your table. 对于XML文件对SQL Server服务可见的情况,这可以使用普通的旧T-SQL进行工作,只需对其进行调整即可插入表中。

DECLARE @DOC INT
DECLARE @XML VARCHAR(MAX) 

-- Dump entire file into variable
SET @XML =
( 
    select BulkColumn from openrowset 
        (BULK N'<your filename>'
            , single_clob) as RuleFile
)

-- Prep doc
EXECUTE sp_xml_preparedocument @DOC OUTPUT, @XML

-- Return data
SELECT * 
    FROM OpenXML(@DOC, '<your xpath>', 2) 
    WITH <your table>

-- Clean up
EXECUTE sp_xml_removedocument @DOC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM