Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml

Syntax

Summary

Defines the core behavior of policies that convert CLR type and member names to XML local names and provides a base for derived classes.

Usage

XmlNamingPolicy converts .NET type and member names to XML names, so you can follow a naming convention without annotating every member. It is the counterpart of JsonNamingPolicy. Assign a policy to one of three properties of XmlSerializerOptions:
The code produces the following output:
A policy of null, the default, leaves names unchanged. The built-in policies are static properties: CamelCase, PascalCase, SnakeCaseLower, SnakeCaseUpper, KebabCaseLower, and KebabCaseUpper. To define your own, derive from XmlNamingPolicy and override ConvertName.

Remarks

There is no identity policy. A null policy on XmlSerializerOptions leaves names unchanged, matching System.Text.Json.JsonNamingPolicy.

Examples

The following examples show the built-in policies, separate policies for elements and attributes, and a policy of your own.

The built-in policies

The code produces the following output:

Different policies for elements and attributes

The code produces the following output:

A custom policy

The code produces the following output:
View the complete sample on GitHub

Best Practices

  • Use the same options to read and to write. The converted names are the ones the serializer looks for when it reads.
  • Prefer a policy to many attributes when a whole format follows one convention, and use [XmlPropertyName] for the exceptions. A name set by an attribute isn’t converted.
  • Make ConvertName deterministic. The serializer calls it for the members of a type when it first uses the type and caches the result with the options, so a policy that returns different names over time gives inconsistent XML between options instances.
  • Return valid XML names from a custom policy: no spaces, and no leading digit.
  • Don’t apply a policy to dictionary keys unless the reader expects the converted keys. The conversion happens when writing and can’t be undone when reading.

Considerations

  • A policy converts type names as well as member names, so it changes the document element. With CamelCase, a type named Warehouse is written as <warehouse>.
  • A policy doesn’t convert names set with [XmlRoot], [XmlPropertyName], [XmlElement], [XmlAttribute], [XmlArray], or [XmlArrayItem], and it doesn’t convert enumeration values.
  • CamelCase and PascalCase change only the first character. CamelCase turns XMLParser into xMLParser. JsonNamingPolicy.CamelCase lowercases the whole leading run of capitals instead.
  • The snake case and kebab case policies insert a separator before a capital that follows a lowercase letter, and before the last capital of a run that is followed by a lowercase letter. XMLParser becomes xml_parser. A digit doesn’t start a new word.
  • In a source-generated context, a policy is named with the XmlKnownNamingPolicy enumeration, because an attribute argument has to be a constant.
  • The built-in policies are shared instances with no state.

Constructors

.ctor Inherited

Inherited from object

Syntax

Properties

CamelCase

Gets the naming policy for camel-casing.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlNamingPolicy A naming policy that converts the first character of a name to lowercase.

KebabCaseLower

Gets the naming policy for lowercase kebab-casing.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlNamingPolicy A naming policy that separates the words of a name with - and converts every character to lowercase.

KebabCaseUpper

Gets the naming policy for uppercase kebab-casing.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlNamingPolicy A naming policy that separates the words of a name with - and converts every character to uppercase.

PascalCase

Gets the naming policy for Pascal-casing.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlNamingPolicy A naming policy that converts the first character of a name to uppercase.

SnakeCaseLower

Gets the naming policy for lowercase snake-casing.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlNamingPolicy A naming policy that separates the words of a name with _ and converts every character to lowercase.

SnakeCaseUpper

Gets the naming policy for uppercase snake-casing.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlNamingPolicy A naming policy that separates the words of a name with _ and converts every character to uppercase.

Methods

ConvertName Abstract

When overridden in a derived class, converts the specified CLR name to an XML local name according to the policy.

Syntax

Parameters

Returns

Type: string The converted XML local name.

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

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 Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.Serialization.XmlKnownNamingPolicy
  • CloudNimble.Text.Xml.Serialization.XmlPropertyNameAttribute
  • CloudNimble.Text.Xml.Serialization.XmlRootAttribute