tags:

views:

40

answers:

2

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);
+4  A: 

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

Greets Flo

Florian Reischl
Thx alot, i ll try XmlWriter.
Kayhano
+1  A: 

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.

Jimmy Hoffa
That's precisely what XmlWriter is. It's implemented on top of a `StringBuilder` or a stream of your choice, and it understands how to properly encode XML. Seriously, look it up: http://msdn.microsoft.com/en-us/library/4d1k42hb(VS.71).aspx
Steven Sudit