Which XML classes to use?
-
Hello everyone! :-D Hey, how should I read and write to XML files? Some tutorials use
XmlDocument
, and some useXmlTextReader
/Writer
... I don't get it... Thanks! ;)Windows Calculator told me I will die at 28. :(
-
Hello everyone! :-D Hey, how should I read and write to XML files? Some tutorials use
XmlDocument
, and some useXmlTextReader
/Writer
... I don't get it... Thanks! ;)Windows Calculator told me I will die at 28. :(
The answer is: it depends! Both XmlDocument and XmlTextReader/Writer are perfectly valid ways to read and write XML files. The reasons for choosing one or the other depend on how the application intends to use the XML files. XmlDocument allows one to add or query XML nodes at random. To do so, it has to load the entire document before it can be used and keep the document in memory. On the other hand, XmlTextReader/Writer are forward-only reader/writers. That is, they visit each node sequentially in a stream-like manner. This makes the classes very fast and light on memory, but at the cost of more limited capabilities and being somewhat more difficult to use. I often find myself using a combination of the two in my applications: XmlDocument to read and extract data from XML documents (because it's easy and flexible) and XmlTextWriter to write XML documents (because it's easy and fast). -Phil
-
The answer is: it depends! Both XmlDocument and XmlTextReader/Writer are perfectly valid ways to read and write XML files. The reasons for choosing one or the other depend on how the application intends to use the XML files. XmlDocument allows one to add or query XML nodes at random. To do so, it has to load the entire document before it can be used and keep the document in memory. On the other hand, XmlTextReader/Writer are forward-only reader/writers. That is, they visit each node sequentially in a stream-like manner. This makes the classes very fast and light on memory, but at the cost of more limited capabilities and being somewhat more difficult to use. I often find myself using a combination of the two in my applications: XmlDocument to read and extract data from XML documents (because it's easy and flexible) and XmlTextWriter to write XML documents (because it's easy and fast). -Phil
Oh, OK, thanks! ;)
Windows Calculator told me I will die at 28. :(