Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.ExceptionSyntax
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 throwsXmlException 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:
Remarks
This type is notSystem.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.Xmlis also imported.CloudNimble.Text.Xml.XmlExceptionandSystem.Xml.XmlExceptionare unrelated, and acatchfor 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
Readagain 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
XmlExceptionas “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
Utf8XmlReaderhasLineNumber,LinePosition, andBytePosition. An exception fromUtf8XmlWriterhas none, because a writer has no input to point into. LinePositioncounts 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.
BytePositioncounts 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.
XmlSerializersetsPath; 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 asSystem.Text.Jsoncounts, when a document is read. When an object is written the path is the elements the writer has open, with no positions.Pathisnullon an exception fromUtf8XmlReader,XmlDocumentorXmlNodeused on its own.- The serializer fills in the position too. An exception for a value that can’t be converted has
BytePosition,LineNumberandLinePositionwhen it comes fromXmlSerializer.Deserialize: they say how far the reader had got. - The message ends with the path. For an exception the library creates,
Messageends withPath: /Order/Lines/Line[1]/@sku | LineNumber: 1 | BytePositionInLine: 55., the formSystem.Text.Jsonuses, 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. Anullargument throwsArgumentNullException, a destination that’s too small forCopyStringthrowsArgumentException, and a disposed writer throwsObjectDisposedException. - The class is sealed and derives from
Exception, not fromSystem.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.ExceptionSyntax
.ctor Inherited
Inherited from
System.ExceptionSyntax
Parameters
.ctor Inherited
Inherited from
System.ExceptionSyntax
Parameters
.ctor Inherited
Inherited from
System.ExceptionSyntax
Parameters
.ctor Inherited
Inherited from
objectSyntax
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.ExceptionSyntax
Property Value
Type:System.Collections.IDictionary
HelpLink Inherited Virtual
Inherited from
System.ExceptionSyntax
Property Value
Type:string?
HResult Inherited
Inherited from
System.ExceptionSyntax
Property Value
Type:int
InnerException Inherited
Inherited from
System.ExceptionSyntax
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 whatSystem.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.ExceptionSyntax
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, asSystem.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.ExceptionSyntax
Property Value
Type:string?
StackTrace Inherited Virtual
Inherited from
System.ExceptionSyntax
Property Value
Type:string?
TargetSite Inherited
Inherited from
System.ExceptionSyntax
Property Value
Type:System.Reflection.MethodBase?
Methods
Equals Inherited Virtual
Inherited from
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
GetBaseException Inherited Virtual
Inherited from
System.ExceptionSyntax
Returns
Type:System.Exception
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
GetObjectData Inherited Virtual
Inherited from
System.ExceptionSyntax
Parameters
GetType Inherited
Inherited from
System.ExceptionSyntax
Returns
Type:System.Type
GetType Inherited
Inherited from
objectSyntax
Returns
Type:System.Type
MemberwiseClone Inherited
Inherited from
objectSyntax
Returns
Type:object
ReferenceEquals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
ToString Override
Inherited from
System.ExceptionSyntax
Returns
Type:string
ToString Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?
Events
SerializeObjectState Inherited
Inherited from
System.ExceptionSyntax
Related APIs
- CloudNimble.Text.Xml.Utf8XmlReader
- CloudNimble.Text.Xml.Utf8XmlWriter
- CloudNimble.Text.Xml.XmlReaderOptions
- CloudNimble.Text.Xml.XmlWriterOptions
- CloudNimble.Text.Xml.XmlDocument
- CloudNimble.Text.Xml.XmlSerializer