The Syndication object model provides rich support for working with extension data—information that is present in a syndication feed's XML representation but not explicitly exposed by classes such as SyndicationFeed and SyndicationItem.
The sample uses the SyndicationFeed class for the purposes of the example. However, the patterns demonstrated in this sample can be used with all of the Syndication classes that support extension data:
This document contains the following pieces of extension data:
The myAttribute attribute of the <feed> element.
<simpleString> element.
<DataContractExtension> element.
<XmlSerializerExtension> element.
<xElementExtension> element.
Writing Extension Data
Attribute extensions are created by adding entries to the AttributeExtensions collection as shown in the following sample code.
//Attribute extensions are stored in a dictionary indexed by
// XmlQualifiedName
feed.AttributeExtensions.Add(new XmlQualifiedName("myAttribute", ""), "someValue");
Element extensions are created by adding entries to the ElementExtensions collection. These extensions can by basic values such as strings, XML serializations of .NET Framework objects, or XML nodes coded by hand.
The following sample code creates an extension element named simpleString.
The XML namespace for this element is the empty namespace ("") and its value is a text node that contains the string "hello, world!".
One way to create complex element extensions that consist of many nested elements is to use the .NET Framework APIs for serialization (both the DataContractSerializer and the XmlSerializer are supported) as shown in the following examples.
feed.ElementExtensions.Add( new DataContractExtension() { Key = "X", Value = 4 } );
feed.ElementExtensions.Add( new XmlSerializerExtension { Key = "Y", Value = 8 }, new XmlSerializer( typeof( XmlSerializerExtension ) ) );
In this example, the DataContractExtension and XmlSerializerExtension are custom types written for use with a serializer.
The SyndicationElementExtensionCollection class can also be used to create element extensions from an XmlReader instance. This allows for easy integration with XML processing APIs such as XElement as shown in the following sample code.
feed.ElementExtensions.Add(new XElement("xElementExtension",
new XElement("Key", new XAttribute("attr1", "someValue"), "Z"),
new XElement("Value", new XAttribute("attr1", "someValue"),
"15")).CreateReader());
Reading Extension Data
The values for attribute extensions can be obtained by looking up the attribute in the AttributeExtensions collection by its XmlQualifiedName as shown in the following sample code.
Console.WriteLine( feed.AttributeExtensions[ new XmlQualifiedName( "myAttribute", "" )]);
Element extensions are accessed using the ReadElementExtensions<T> method.
foreach( string s in feed2.ElementExtensions.ReadElementExtensions<string>("simpleString", ""))
{
Console.WriteLine(s);
}
foreach (DataContractExtension dce in feed2.ElementExtensions.ReadElementExtensions<DataContractExtension>("DataContractExtension",
"http://schemas.datacontract.org/2004/07/SyndicationExtensions"))
{
Console.WriteLine(dce.ToString());
}
foreach (XmlSerializerExtension xse in feed2.ElementExtensions.ReadElementExtensions<XmlSerializerExtension>("XmlSerializerExtension", "", new XmlSerializer(typeof(XmlSerializerExtension))))
{
Console.WriteLine(xse.ToString());
}
It is also possible to obtain an XmlReader at individual element extensions by using the GetReader() method.
Finance and operations apps are customized by using extensions, which let you add functionality to model elements and source code in the Application Object Tree (AOT) by using Visual Studio.