Class XmpData
A parsed XMP packet: its properties by namespace URI and local name, each a simple value, an XmpArray or an XmpStruct, with typed getters and setters for what apps ask for most (Dublin Core, XMP Basic, Photoshop and TIFF properties). An unedited packet writes back byte for byte; an edited one is written as canonical RDF/XML with padding, keeping every property it does not know, qualifiers included.
public sealed class XmpData : XmpStruct
- Inheritance
-
XmpData
- Inherited Members
Examples
XmpData xmp = new()
{
Title = "Harbour at dawn",
Creators = ["A. Photographer"],
Keywords = ["harbour", "Bergen"],
Rating = 4,
};
// Any property, by namespace URI and local name; structures and arrays nest.
XmpStruct contact = new();
contact.Set(new XmpName(XmpNamespaces.IptcCore, "CiEmailWork"), "photo@example.com");
xmp.Set(new XmpName(XmpNamespaces.IptcCore, "CreatorContactInfo"), contact);
if (xmp.TryGet(new XmpName(XmpNamespaces.DublinCore, "title"), out object? title) && title is XmpArray languages)
{
languages.SetLanguage("nb-NO", "Havn ved daggry");
}
ReadOnlyMemory<byte> packet = xmp.ToBytes(); // canonical RDF/XML with padding
Constructors
XmpData()
Creates an empty packet, to fill with the setters.
public XmpData()
Properties
City
Gets or sets the city shown (photoshop:City). Setting null removes it.
public string? City { get; set; }
Property Value
Country
Gets or sets the country shown (photoshop:Country). Setting null removes it.
public string? Country { get; set; }
Property Value
CreateDate
Gets or sets when the content was created (xmp:CreateDate). Setting null removes it.
public DateTimeOffset? CreateDate { get; set; }
Property Value
CreatorTool
Gets or sets the software that made the file (xmp:CreatorTool). Setting null removes it.
public string? CreatorTool { get; set; }
Property Value
Creators
Gets or sets the creators (dc:creator), in order. Setting an empty list removes them.
public IReadOnlyList<string> Creators { get; set; }
Property Value
Credit
Gets or sets the provider credited (photoshop:Credit). Setting null removes it.
public string? Credit { get; set; }
Property Value
Description
Gets or sets the description or caption (dc:description), the x-default item. Setting null removes it.
public string? Description { get; set; }
Property Value
Headline
Gets or sets the headline (photoshop:Headline). Setting null removes it.
public string? Headline { get; set; }
Property Value
IsDamaged
Gets whether the packet could not be read: XML that is not well-formed, no rdf:RDF, or more data than allowed. It then has no properties, and its original bytes are written back unless properties are set.
public bool IsDamaged { get; }
Property Value
Keywords
Gets or sets the keywords (dc:subject). Setting an empty list removes them.
public IReadOnlyList<string> Keywords { get; set; }
Property Value
Label
Gets or sets the color label (xmp:Label), such as "Red". Setting null removes it.
public string? Label { get; set; }
Property Value
ModifyDate
Gets or sets when the file was last changed (xmp:ModifyDate). Setting null removes it.
public DateTimeOffset? ModifyDate { get; set; }
Property Value
Orientation
Gets or sets the orientation (tiff:Orientation), the XMP copy of EXIF's; null when absent or not 1 to 8. Setting null removes it.
public ImageOrientation? Orientation { get; set; }
Property Value
Rating
Gets or sets the rating (xmp:Rating): -1 for rejected, 0 for unrated, 1 to 5 stars. Setting null removes it.
public int? Rating { get; set; }
Property Value
- int?
Exceptions
- ArgumentOutOfRangeException
The value set is not -1 to 5.
Rights
Gets or sets the rights statement (dc:rights), the x-default item. Setting null removes it.
public string? Rights { get; set; }
Property Value
Source
Gets or sets the original owner (photoshop:Source). Setting null removes it.
public string? Source { get; set; }
Property Value
Title
Gets or sets the title (dc:title), the x-default item of its language alternatives. Setting null removes it.
public string? Title { get; set; }
Property Value
Warnings
Gets what was wrong with the packet when read, as sentences.
public IReadOnlyList<string> Warnings { get; }
Property Value
Methods
Copy()
Returns an independent copy.
public XmpData Copy()
Returns
- XmpData
The copy.
Parse(ReadOnlySpan<byte>, SafetyLimits?)
Reads an XMP packet (RDF/XML, with or without its xpacket wrapper). Never throws: a packet that is not well-formed or is over MaxMetadataBytes is marked damaged and keeps its bytes. DTDs are refused and nothing is resolved, so external entities and entity expansion cannot run.
public static XmpData Parse(ReadOnlySpan<byte> packet, SafetyLimits? limits = null)
Parameters
packetReadOnlySpan<byte>The packet's bytes, UTF-8 or UTF-16.
limitsSafetyLimitsThe limits whose MaxMetadataBytes caps the packet. Defaults to Default.
Returns
Examples
// An XMP packet from a sidecar file or a database; DTDs are refused and nothing outside it is read.
XmpData xmp = XmpData.Parse(packet.Span, SafetyLimits.Default with { MaxMetadataBytes = 1024 * 1024 });
Console.WriteLine(xmp.IsDamaged ? string.Join(" ", xmp.Warnings) : $"Label: {xmp.Label}");
ToBytes()
Returns the packet: the original bytes when unedited, otherwise written as canonical RDF/XML with padding.
public ReadOnlyMemory<byte> ToBytes()
Returns
- ReadOnlyMemory<byte>
The bytes.