Class DOMParser
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
The DOMParser interface provides the ability to parse {{Glossary("XML")}} or {{Glossary("HTML")}} source code from a string into a DOM Document.
[Value("DOMParser")]
public class DOMParser
- Inheritance
-
DOMParser
- Inherited Members
Remarks
You can perform the opposite operation—converting a DOM tree into XML or HTML source—using the XMLSerializer interface.
In the case of an HTML document, you can also replace portions of the DOM with new DOM trees built from HTML by setting the value of the InnerHTML and OuterHTML properties. These properties can also be read to fetch HTML fragments corresponding to the corresponding DOM subtree.
Note that XMLHttpRequest can parse XML and HTML directly from a URL-addressable resource, returning a Document in its Response property.
NOTE
Be aware that block-level elements like
<p>will be automatically closed if another block-level element is nested inside and therefore parsed before the closing</p>tag.
-Parsing and serializing XML
-XMLHttpRequest
-XMLSerializer
-JSONParse - counterpart for JSON documents.
Constructors
DOMParser()
The DOMParser() constructor creates a new DOMParser object. This object can be used to parse the text of a document using the parseFromString() method.
public DOMParser()
Remarks
Methods
ParseFromString(Union91, DOMParserSupportedType)
WARNING
This method parses its input as HTML, writing the result into the DOM.
APIs like this are known as injection sinks, and are potentially a vector for cross-site scripting (XSS) attacks, if the input originally came from an attacker.
[Value("parseFromString")]
public Document ParseFromString(Union91 string_, DOMParserSupportedType type)
Parameters
string_Union91typeDOMParserSupportedType
Returns
- Document
A Document with DocumentcontentType matching the given
mimeType.NOTE
The browser may actually return an HTMLDocument or XMLDocument object.
These derive from Document and add no attributes: they are essentially equivalent.
Remarks
You can mitigate this risk by always passing TrustedHTML objects instead of strings and enforcing trusted types.
See Security considerations for more information.
The parseFromString() method of the DOMParser interface parses an input containing either HTML or XML, returning a Document with the type given in the DocumentcontentType property.
NOTE
The
Document.parseHTMLUnsafe()static method provides an ergonomic alternative for parsing HTML markup into a Document.
-XMLSerializer
-JSONParse - counterpart for JSON documents.