System.Xml.XmlDictionaryAttribute
Namespace: System.Xml
Assembly: System.Runtime.Serialization.dll
Overview
Syntax
Members
Examples
The XmlDictionaryAttribute
class is used to specify how a property or field is
serialized into a dictionary-based XML format. It can be applied to properties, fields,
or parameters to control the dictionary key used during serialization.
Applies to: Property, Field, Parameter
public sealed class XmlDictionaryAttribute : Attribute { public XmlDictionaryAttribute(); public XmlDictionaryAttribute(string key); public string Key { get; set; } }
Constructors
Signature | Description |
---|---|
XmlDictionaryAttribute() |
Initializes a new instance with the default empty key. |
XmlDictionaryAttribute(string key) |
Initializes a new instance with the specified dictionary key. |
Properties
Name | Type | Description |
---|---|---|
Key |
string |
The dictionary key used for serialization. If empty, the member name is used. |
using System; using System.Xml; using System.Runtime.Serialization; public class Person { [XmlDictionary("FirstName")] public string Name { get; set; } [XmlDictionary("Age")] public int Age { get; set; } } var person = new Person { Name = "Alice", Age = 30 }; var writer = XmlDictionaryWriter.CreateBinaryWriter(Console.OpenStandardOutput()); var serializer = new DataContractSerializer(typeof(Person)); serializer.WriteObject(writer, person); writer.Flush();
This example demonstrates how to assign custom dictionary keys to class members using the
XmlDictionaryAttribute
.