files/en-us/web/api/xmlserializer/serializetostring/index.md
{{APIRef("HTML DOM")}}
The {{domxref("XMLSerializer")}} method
serializeToString() constructs a string representing the
specified {{Glossary("DOM")}} tree in {{Glossary("XML")}} form.
serializeToString(rootNode)
rootNode
A string containing the XML representation of the specified DOM tree.
rootNode is not a compatible node type. The root node
must be either {{domxref("Node")}} or {{domxref("Attr")}}.InvalidStateError {{domxref("DOMException")}}
SyntaxError {{domxref("DOMException")}}
The specified root node—and all of its descendants—must be compatible with the XML serialization algorithm. The root node itself must be either a {{domxref("Node")}} or {{domxref("Attr")}} object.
The following types are also permitted as descendants of the root node, in addition to
Node and Attr:
If any other type is encountered, a {{jsxref("TypeError")}} exception is thrown.
There are some things worth noting about the XML output by
serializeToString():
Element and Attr nodes are always
serialized with their {{domxref("Element.namespaceURI", "namespaceURI")}} intact. This
may mean that a previously-specified {{domxref("Element.prefix", "prefix")}} or default
namespace may be dropped or altered."<someelement></someelement>") instead of using the
empty-element tag ("<someelement/>").{{Specifications}}
{{Compat}}