开发者

Save all xml nodes to db without looping through it

开发者 https://www.devze.com 2023-02-03 10:24 出处:网络
I have this xml: <Path> <Record> <ID>6534808</ID> <Distance>1.05553036073736</Distance>

I have this xml:

<Path>
  <Record>
     <ID>6534808</ID>
     <Distance>1.05553036073736</Distance>
  </Record>
  <Record>
     <ID>6542471</ID>
     <Distance>1.05553036073736</Distance>
  </Record>
  ... and about more 500 nodes
</Path>

And I'm using the code below to get all "Record" nodes开发者_运维问答:

XmlNodeList paths = xDoc.SelectNodes("//Record");

And, after that, I save each record to database. But, the problem is that I'm using a foreach to loop through this nodes and the count of this nodes may be more than 500, sometimes it gets up to 1000 "Records" node. And this gets too long...

Is there a way to save all of these nodes without looping through it?

Thanks!!


Maybe you can iterate with your current for-each, but my suggestion is don't save the data to a record per each iteration.

First create the insert statements, then batch execute all inserts in a single trip. This is more efficient.

In the other hand, maybe you're using some OR/M. If this is your situation, and your choice is NHibernate, it supports batching commands.

You've even another cleaner, and efficient solution: XSLT.

Convert your XML to SQL with a very simple XSLT instead of iterating nodes from C#. Parsing your XML to SQL with that XSLT is the fastest and best solution, and you can send the data in a single trip too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号