Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.XmlSyntax
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:
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. Anull 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
Different policies for elements and attributes
A custom policy
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
ConvertNamedeterministic. 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 namedWarehouseis 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. CamelCaseandPascalCasechange only the first character.CamelCaseturnsXMLParserintoxMLParser.JsonNamingPolicy.CamelCaselowercases 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.
XMLParserbecomesxml_parser. A digit doesn’t start a new word. - In a source-generated context, a policy is named with the
XmlKnownNamingPolicyenumeration, because an attribute argument has to be a constant. - The built-in policies are shared instances with no state.
Constructors
.ctor Inherited
Inherited from
objectSyntax
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
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
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 Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?
Related APIs
- CloudNimble.Text.Xml.XmlSerializerOptions
- CloudNimble.Text.Xml.Serialization.XmlKnownNamingPolicy
- CloudNimble.Text.Xml.Serialization.XmlPropertyNameAttribute
- CloudNimble.Text.Xml.Serialization.XmlRootAttribute