What difference xmldocument vs xmlwriter?

Asked

Viewed 222 times

2

What is the difference between the two classes?

In which situation each fits best?

2 answers

6

Xmldocument represents an entire XML loaded into memory, while Xmlwriter is an object that assists in writing XML into a stream.

Xmldocument

If you want to manipulate an XML, for example, load it from a file, change some part of its structure, and then save it, then I would use Xmldocument for that. With it is possible locate the elements using Xpath, which greatly facilitates manipulation.

Xdocument

This class and its related ones are part of . Net from version 3.5 onwards, much easier to work with than with Xmldocument and related. If you want to create applications without taking the previous versions of . net then this is the recommended way.

To create an XML structure, you only need a statement, instead of inserting nodes:

var xdocument = new XDocument(
    new XElement("elementoRaiz",
        new XAttribute("atributo1", "valor"),
        new XElement("elementoFilho", "texto do elemento filho"),
        new XElement("elementoFilho2", "texto do elemento filho 2")
    ));

Xmlwriter

If you just want to write an Xml in a file, for example, reading data from any source such as a database, you could use Xmlwriter to write the XML as you read the data from the database.

  • Thank you @Miguel, could you also explain about Xdocument as well ? in a brief search, Xdocument looks like the most organized and beautiful syntax, rsrsrs

0

Xmldocument is a more complex class than Xmlwriter, as it contains more methods for handling xml while Xmlwriter basically serves to record this xml.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.