简体   繁体   中英

How to run an XmlReader in parallel in .NET?

I have a potentially large xml document to read into memory in the most performant way possible. The structure of the document is that it containst a large number of fairly simple xml elements.

Prior research and testing (single threaded) has shown that XmlReader is the most performance oriented class to use.

The nodes/elements I am interested in are small in and of themselves and don't require too much processing such that about 60% of the time is spent just doing the xmlReader.Read() command moving to the next node.

What kind of approach could I take to bring parallelism to bear on this problem st performance will be improved (more than the overhead that parallelism will bring)?

One half formed idea I have is that if there was a way to efficiently divide the xml into large chunks st it could be fed into different worker threads...but I'm not sure how to divide the document without parsing it!

Example XML

Extract taken from the body of one of the main xml files in an Excel spreadsheet. I am interested in the "c" elements:

<row r="1" spans="1:1" x14ac:dyDescent="0.4"><c r="A1"><v>1</v></c></row>
<row r="2" spans="1:1" x14ac:dyDescent="0.4"><c r="A2"><f>A1+1</f><v>2</v></c></row>
<row r="3" spans="1:1" x14ac:dyDescent="0.4"><c r="A3"><f t="shared" ref="A3:A66" si="0">A2+1</f><v>3</v></c></row>
<row r="4" spans="1:1" x14ac:dyDescent="0.4"><c r="A4"><f t="shared" si="0"/><v>4</v></c></row>
<row r="5" spans="1:1" x14ac:dyDescent="0.4"><c r="A5"><f t="shared" si="0"/><v>5</v></c></row>
<row r="6" spans="1:1" x14ac:dyDescent="0.4"><c r="A6"><f t="shared" si="0"/><v>6</v></c></row>
<row r="7" spans="1:1" x14ac:dyDescent="0.4"><c r="A7"><f t="shared" si="0"/><v>7</v></c></row>
<row r="8" spans="1:1" x14ac:dyDescent="0.4"><c r="A8"><f t="shared" si="0"/><v>8</v></c></row>
<row r="9" spans="1:1" x14ac:dyDescent="0.4"><c r="A9"><f t="shared" si="0"/><v>9</v></c></row>

您可以尝试使用Parallel LINQ to XML并行访问XML。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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