XElement constructor extremly slow with byte[] array
Recently I’ve tried to build a xml document using Linq to XML classes like XDocument
and XElement
One element has to contain byte array. I’ve checked
XElement(XName name, object content)
constructor and found that it works fine for DateTime
, string
decimal
or bool
.
Then I’ve wrote something like code below:
(I will show the GetBytes
method later but it returns requested array of bytes)
For small arrays (up to 100KB) code above works fine. However I’ve tried to put 512KB using this approach and program halted.
I’ve made some investigations trying to put byte array to the xml and below are results:
testB in 2592ms (50KB)
testB in 11514ms (100KB)
testB in 29173ms (150KB)
testB in 40936ms (175KB)
it means that to execute XElement
constructor with 175KB byte array takes about 40 seconds (!)
I’ve checked XElement
source code and found that this behaviour is caused by string concatenation
The AddStringSkipNotify
method is executed for each element of the byte array (IEnumerable
object passed to the XElement
content parameter):
To resolve this issue I converted byte array to the Base64
string and pass a sting as content
parameter value: