Class XMLParser
Reads XML into elements: their names, their namespaces, their attributes in the order they are written, and their text. It is the one parser of the four ports and of PDFjet Pro, which reads its electronic invoices with it, and SVGImage its images, so that they read a document alike. It reads what such a document is made of and no more, and what it leaves out is what makes XML dangerous to read:
- A document type declaration, a DOCTYPE, is refused by Parse, and skipped by ParseSkippingDoctype, as the files of the drawing programs have one; either way its entities are not read: an entity that names a file or a URL reads what it should not, and the ones that stand for each other grow a short document into gigabytes.
- The entities are the five of XML, <, >, &, " and ', and the numeric ones, such as   and  . Any other is an error, the ones a DOCTYPE declares among them.
- The elements nest MAX_DEPTH levels at most, and the reading does not recurse, so no document overflows the stack.
- The document is 20 MB at most, and has MAX_ELEMENTS elements at most.
- The namespaces are looked up in the elements that declare them, from the innermost out, and not copied into each element, so that no number of declarations makes the reading slower than the length of the document times its depth.
- Nothing is fetched, and no schema is read.
What is read is XML, and what is not is an error: a character that XML does not have, such as U+0000 or U+FFFF, written or as an entity; an attribute written twice, or not after a space; and a prefix that no element declares. A line break is a line feed, as XML reads a carriage return and the line feed after it, and the tabs and line breaks in the value of an attribute are spaces, as XML normalizes them.
The document is UTF-8, or UTF-16 when it starts with a byte order mark; another encoding in the declaration is an error. The bytes that are not valid UTF-8 are the character U+FFFD, one for each part of a sequence that is as much of a character as it is valid for, which is what the Unicode Standard recommends and the decoder of .NET does.
public sealed class XMLParser
- Inheritance
-
XMLParser
- Inherited Members
Fields
MAX_DEPTH
How deep the elements of a document may nest.
public const int MAX_DEPTH = 256
Field Value
MAX_ELEMENTS
How many elements a document may have, a million: an invoice of the 20 MB has fewer than half as many, and an SVG image has its points in the attributes of its paths, not in elements. It keeps a document of tiny elements from taking hundreds of megabytes (the review of 9 October 2026).
public const int MAX_ELEMENTS = 1000000
Field Value
MAX_SIZE
How many bytes a document may be, 20 MB.
public const int MAX_SIZE = 20971520
Field Value
TOO_LARGE
The message of a document of more than 20 MB.
public const string TOO_LARGE = "The document is more than 20 MB."
Field Value
Methods
Parse(byte[])
Reads the document and returns its root element. A document type declaration is refused.
public static XMLNode Parse(byte[] bytes)
Parameters
bytesbyte[]the document.
Returns
- XMLNode
the root element.
Exceptions
- XMLException
The document is not the XML this reads, or is more than 20 MB.
Parse(Stream)
Reads the document of the stream and returns its root element. The stream is read to its end, or to the 20 MB a document may be, and is not closed.
public static XMLNode Parse(Stream stream)
Parameters
streamStreamthe stream.
Returns
- XMLNode
the root element.
Exceptions
- IOException
The stream cannot be read.
- XMLException
The document is not the XML this reads, or is more than 20 MB.
ParseSkippingDoctype(byte[])
Reads the document and returns its root element, as Parse does, but skips a document type declaration, whose entities are not read: as the SVG files of the drawing programs have one. A prefix that no element declares is read as no namespace, as SVG files copied from web pages have xlink:href without xmlns:xlink, rather than an error.
public static XMLNode ParseSkippingDoctype(byte[] bytes)
Parameters
bytesbyte[]the document.
Returns
- XMLNode
the root element.
Exceptions
- XMLException
The document is not the XML this reads, or is more than 20 MB.
ParseSkippingDoctype(Stream)
Reads the document of the stream and returns its root element, as Parse(Stream) does, but skips a document type declaration, whose entities are not read.
public static XMLNode ParseSkippingDoctype(Stream stream)
Parameters
streamStreamthe stream.
Returns
- XMLNode
the root element.
Exceptions
- IOException
The stream cannot be read.
- XMLException
The document is not the XML this reads, or is more than 20 MB.
Trim(string)
Returns the text without the characters up to and including the space at its two ends, as the trim of Java does, and not the characters that Unicode calls whitespace, as the Trim of C# does.
public static string Trim(string text)
Parameters
textstringthe text.
Returns
- string
the text trimmed.