简体   繁体   中英

Need advice for XMLText usage in C#

I am writing an output module to visualize my data on another program.

If you inspect the code, i add point coordinates to a string and then add this string as a TextNode to another element. Problem is number of points can be more than 500000

Is there a better way to write a lot of text to xml or is this OK?

XMLElement points = doc.CreateElement("VTK", "DataArray");

string str = "";

for (int i = 0; i < sim.NumberOfParticles; i++)
{
str += sim.pPosX[i].ToString() + " " + sim.pPosY[i] + " " + sim.pPosZ[i] + "\n"
}
XmlText coordinates = doc.CreateTextNode(str);
points.AppendChild(coordinates);

For this amount of data, use the XmlWriter instead of a XmlDocument . It streams and resources are no problem any more.

Flo's right, but furthermore your whole iterative mechanism is a little confusing and could probably be written in a more natural way, creating your XML objects instead of using some string concatenation.

One really heavy thing you're doing here though is thrashing your memory with strings when you should use a stringbuilder, and with heuristics you could probably even default the stringbuilder size accurately, or close to it by multiplying your numberofparticles by the length generic length of the string that will be generated for each particle.

If you're going to stick with string concatenation in that for, definitely use the stringbuilder, otherwise I would suggest using proper XML objects made for holding XML data instead of strings.

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