Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.Exception

Syntax

Summary

The exception that is thrown when XML is not well formed, a reader limit is exceeded, or a value cannot be read as the requested type. This class cannot be inherited.

Usage

The library throws XmlException for every problem with XML itself: input that isn’t well formed, a limit that’s exceeded, a value that can’t be read as the type you asked for, and output that the writer refuses to produce. Catch it around the code that reads or writes. When the exception comes from Utf8XmlReader, it says where the reader was:
View the complete sample on GitHub

Remarks

This type is not System.Xml.XmlException and does not wrap it. The location properties are optional. They are set when the reader knows the byte offset and line.

Best Practices

  • Catch this type by its full name when System.Xml is also imported. CloudNimble.Text.Xml.XmlException and System.Xml.XmlException are unrelated, and a catch for one doesn’t catch the other.
  • Log the line and the position with the message. The message alone doesn’t say where the problem is.
  • Treat an exception from a reader as the end of that document. Don’t call Read again on a reader that threw.
  • Discard the output when a writer throws. Bytes that were committed before the error are already in your buffer or stream.
  • Don’t parse the message. To react to one condition, such as a document that’s too deep, set the limit you care about and treat any XmlException as “this document isn’t acceptable”.
  • Don’t show the message of an exception to an untrusted caller without thought. It describes the structure of the input, and for a declared encoding it repeats a name from the input.

Considerations

  • The location properties are optional. They’re nullable, and they’re set only when the code that throws knows a location. An exception from Utf8XmlReader has LineNumber, LinePosition, and BytePosition. An exception from Utf8XmlWriter has none, because a writer has no input to point into.
  • LinePosition counts bytes, not characters. On a line with characters outside ASCII, it’s larger than the column an editor shows.
  • The location is where the reader stopped, which is at the problem or right after it. For a mismatched end tag, that’s the end of the tag.
  • BytePosition counts from the start of the document, including the blocks before the current one when you read in blocks.
  • Lines are counted after line ends are normalized, so a carriage return followed by a line feed is one line end.
  • XmlSerializer sets Path; the reader on its own does not. An exception that is thrown while the serializer reads or writes names each element from the document element down to the error: /Order/Lines/Line[1]/@sku. An item of a collection has its position after its name, counted from 0 as System.Text.Json counts, when a document is read. When an object is written the path is the elements the writer has open, with no positions. Path is null on an exception from Utf8XmlReader, XmlDocument or XmlNode used on its own.
  • The serializer fills in the position too. An exception for a value that can’t be converted has BytePosition, LineNumber and LinePosition when it comes from XmlSerializer.Deserialize: they say how far the reader had got.
  • The message ends with the path. For an exception the library creates, Message ends with Path: /Order/Lines/Line[1]/@sku | LineNumber: 1 | BytePositionInLine: 55., the form System.Text.Json uses, or with the path alone when no line is known. An exception you create keeps the message you gave it.
  • A path you supply is kept. The constructor accepts a path, and the serializer does not change it.
  • Not every failure is an XmlException. A null argument throws ArgumentNullException, a destination that’s too small for CopyString throws ArgumentException, and a disposed writer throws ObjectDisposedException.
  • The class is sealed and derives from Exception, not from System.Xml.XmlException.

Constructors

.ctor

Initializes a new instance of the XmlException class with a specified error message and no location.

Syntax

Parameters

.ctor

Initializes a new instance of the XmlException class with a specified error message and a reference to the inner exception that is the cause of this exception.

Syntax

Parameters

.ctor

Initializes a new instance of the XmlException class with a specified error message and the location of the error in the document.

Syntax

Parameters

.ctor Inherited

Inherited from System.Exception

Syntax

.ctor Inherited

Inherited from System.Exception

Syntax

Parameters

.ctor Inherited

Inherited from System.Exception

Syntax

Parameters

.ctor Inherited

Inherited from System.Exception

Syntax

Parameters

.ctor Inherited

Inherited from object

Syntax

Properties

BytePosition

Gets the zero-based byte offset in the UTF-8 document at which the error occurred.

Syntax

Property Value

Type: long? The zero-based byte offset in the UTF-8 document, or null if it is not known.

Data Inherited Virtual

Inherited from System.Exception

Syntax

Property Value

Type: System.Collections.IDictionary
Inherited from System.Exception

Syntax

Property Value

Type: string?

HResult Inherited

Inherited from System.Exception

Syntax

Property Value

Type: int

InnerException Inherited

Inherited from System.Exception

Syntax

Property Value

Type: System.Exception?

LineNumber

Gets the one-based number of the line on which the error occurred.

Syntax

Property Value

Type: long? The one-based line number, counted after end-of-line normalization, or null if it is not known.

LinePosition

Gets the one-based byte position within the line at which the error occurred.

Syntax

Property Value

Type: long? The one-based UTF-8 byte offset in the line, or null if it is not known.

Message Override

Gets the message that describes the error.

Syntax

Property Value

Type: string The message. When the library made the exception and XmlException.Path is set, the path follows the message, and the line and the position in the line follow the path when they are known: The value is not a valid integer. Path: /Order/Lines[1]/Qty | LineNumber: 4 | BytePositionInLine: 50.

Remarks

This is what System.Text.Json does with JsonException.Message. The message of an exception that your code makes, in a converter for example, is left as you wrote it.

Message Inherited Virtual

Inherited from System.Exception

Syntax

Property Value

Type: string

Path

Gets the path in the document at which the error occurred.

Syntax

Property Value

Type: string? The document path, such as /Order/Lines/Line[1]/@sku, or null if it is not known.

Remarks

XmlSerializer sets the path of an exception that is thrown while it reads or writes, as System.Text.Json sets JsonException.Path. The path starts at the document element and names each element on the way to the error. An attribute is @name and the text of an element is text(). When a document is read, an item of a collection has its position in the collection after its name, counted from 0 as System.Text.Json counts: Line[1] is the second item. When an object is written, the path is made of the elements the writer has open, and has no positions. A path that was given to the constructor is left as it is.

Source Inherited Virtual

Inherited from System.Exception

Syntax

Property Value

Type: string?

StackTrace Inherited Virtual

Inherited from System.Exception

Syntax

Property Value

Type: string?

TargetSite Inherited

Inherited from System.Exception

Syntax

Property Value

Type: System.Reflection.MethodBase?

Methods

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetBaseException Inherited Virtual

Inherited from System.Exception

Syntax

Returns

Type: System.Exception

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

GetObjectData Inherited Virtual

Inherited from System.Exception

Syntax

Parameters

GetType Inherited

Inherited from System.Exception

Syntax

Returns

Type: System.Type

GetType Inherited

Inherited from object

Syntax

Returns

Type: System.Type

MemberwiseClone Inherited

Inherited from object

Syntax

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

ToString Override

Inherited from System.Exception

Syntax

Returns

Type: string

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?

Events

SerializeObjectState Inherited

Inherited from System.Exception

Syntax

  • CloudNimble.Text.Xml.Utf8XmlReader
  • CloudNimble.Text.Xml.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlReaderOptions
  • CloudNimble.Text.Xml.XmlWriterOptions
  • CloudNimble.Text.Xml.XmlDocument
  • CloudNimble.Text.Xml.XmlSerializer