> The attribute takes up exactly the same amount
of memory as the
> element.
Not true!
I ran a small C# program that shows the sizes of a bare (just
created) XmlDocument, XmlElement and XmlAttribute.
Here are the results:
an XmlDocument: 3176 bytes
an XmlElement: 656 bytes
an XmlAttribute: 152 bytes.
Thus an XmlElement takes more than 4 times the memory that
an XmlAttribute takes.
So, when memory is important (and it almost always is!),
converting elements to attributes can significantly reduce the
memory footprint of an XML processing application.
P.S.
These results may vary for different XML DOM implementations,
but I expect them to be similar -- maybe other people could
share their results?
Here is the code I used:
using System;
using System.Xml;
class Program
{
static void Main(string[] args)
{
var mem1 = GC.GetTotalMemory(true);
var doc = new XmlDocument();
var mem2 = GC.GetTotalMemory(true);
var elem = doc.CreateElement("x");
var mem3 = GC.GetTotalMemory(true);
var attr = doc.CreateAttribute("y");
var mem4 = GC.GetTotalMemory(true);
Console.WriteLine($"XmlDocument: {mem2 - mem1}
bytes");
Console.WriteLine($"XmlElement: {mem3 - mem2} bytes");
Console.WriteLine($"XmlAttribute: {mem4 - mem3}
bytes");
}
}
And here is the result:
XmlDocument: 3176 bytes
XmlElement: 656 bytes
XmlAttribute: 152 bytes
Cheers,
Dimitre
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant
intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be
the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know
whether what you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in
200yrs.Will they write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to
enjoy it.