- Mixed content: text with elements inside it, such as
Press <Key>Ctrl</Key> to save. - CDATA sections: text that is written without escaping, such as a script or a query.
- Unmapped content: elements and attributes that your type doesn’t declare, which you want to keep and write back. System.Text.Json calls this overflow or extension data.
Map text mixed with elements
Mark aList<XmlMixedContentItem> member with [XmlMixed]. The serializer fills the list with the content of the element, in document order. Each item is one of two types:
Paragraph.cs
Program.cs
<Key> element are part of the text runs, so the sentence is written back exactly as it was read.
Know the following before you rely on mixed content:
- A type can have one
[XmlMixed]member. It can also have attribute members, but no element members and no[XmlText]or[XmlCData]member. The serializer throwsNotSupportedExceptionfor any other combination. - Child elements are DOM nodes, not typed objects. Read them with the
XmlNodeAPI, or pass a node toXmlSerializer.Deserialize<T>(XmlNode)to turn it into an object. - Text that is only white space between two elements is dropped, unless you set
XmlSerializerOptions.WhitespaceHandlingtoXmlWhitespaceHandling.Significant. In<Paragraph><Key>A</Key> <Key>B</Key></Paragraph>, the space between the keys is lost with the default setting. - A CDATA section is read as an
XmlMixedText, and is written back as ordinary escaped text. Comments and processing instructions are not kept. WriteIndentedis safe to use. The writer doesn’t indent inside an element that contains text, so the text runs are written unchanged.
Write a member as CDATA
A CDATA section holds text in which< and & don’t need to be escaped. It makes embedded code easier for a person to read. To a parser, it is the same text as the escaped form.
Mark a member with [XmlCData] to make it the text of its containing element, written as a CDATA section.
Script.cs
Program.cs
[XmlCData] follows the rules of [XmlText], which maps a member to the element’s text without CDATA:
- The member is the content of the containing element. It doesn’t get an element of its own, so the type can’t also have members that are child elements. The other members must be attributes.
- A type can have one
[XmlText]or[XmlCData]member, and the member must have a simple type, such asstring, a number, or an enumeration. - When reading, the serializer accepts escaped text and CDATA alike. The attribute only affects writing.
- A value that contains
]]>can’t be written as one CDATA section, so the serializer splits it at each]]>and writes several sections:a]]>bis written as<![CDATA[a]]]]><![CDATA[>b]]>. A reader joins them again, and the value reads back as it was. - A
nullvalue writes no content, which leaves the element empty.
Keep content that no member maps
By default, the serializer skips an element or attribute that your type doesn’t map. That is safe for reading, but the content is gone when you write the object back. To keep it, add a member of typeXmlExtensionData and mark it with [XmlExtensionData].
This is the counterpart of [JsonExtensionData]. JSON overflow is a dictionary of properties. XML overflow has two parts, because an element can carry both attributes and child elements:
Settings.cs
Program.cs
Theme, and the document survives the round trip with the attributes, the Telemetry element, and the namespaced Font element intact.
Details that matter:
- The member must be of type
XmlExtensionData, and a type can have only one. Any other member type throwsNotSupportedException. - If the document has no unmapped content, the serializer doesn’t assign the member. It stays
nullunless your type creates the object itself. If your type does create it, captured content is added to the object that is already there. - Namespace declarations (
xmlnsandxmlns:prefix) are not captured. Neither are the attributes the serializer handles itself:xsi:nil,xsi:type, and the reference attributes described in Preserve references. - When writing, unmapped attributes follow the mapped attributes, and unmapped elements follow the mapped elements. The original position of an unmapped element among the mapped ones is not recorded.
- A namespaced attribute keeps the prefix it was read with. An attribute you add in code with an
XmlNamethat has a namespace but no prefix gets a generated prefix. - You can add to the object in code before you serialize. That is a way to write attributes or elements that the type doesn’t declare.
Reject content that no member maps
When a type has no extension data member,XmlSerializerOptions.UnmappedMemberHandling decides what happens to unmapped content. The default, XmlUnmappedMemberHandling.Skip, ignores it. XmlUnmappedMemberHandling.Disallow treats it as an error, as JsonUnmappedMemberHandling.Disallow does.
Program.cs
Unknown attribute 'a'. Path: /Contact | LineNumber: 1 | BytePositionInLine: 15. The XmlSerializerOptions.Strict preset sets Disallow for you.
An extension data member takes precedence. A type that has one accepts unmapped content even when the options say Disallow, because the content is mapped: it goes to that member.
View the complete sample on GitHub
See also
Customize names
Map members to attributes, elements, and element text.
Use XmlNode
Read and build the element nodes that mixed content and extension data hold.
Configure options
Set
UnmappedMemberHandling and the other serializer options.XmlExtensionData
API reference for the extension data type.