XObject and Annotations

XObject represents the base class of the whole LINQ to XML API, and it mainly provides methods and properties to work with annotations on nodes. Annotations are a new mechanism that maps metadata to XML nodes. For instance, we can add custom user information to our
nodes

Annotations applied to an XElement instance

XElement customer = XElement.Load(@"..\..\customer.xml");
CustomerAnnotation annotation = new CustomerAnnotation();
annotation.Notes = "This is a good customer!";
customer.AddAnnotation(annotation);

CustomerAnnotation is a custom type and can be any .NET type. We can then retrieve annotationsfrom XML nodes by using one of the two generic methods, Annotation and Annotations.These generic methods search for an annotation of type T or one that is derived from T in the current node, and if one exists, Annotation and Annotations return the first one or the full set of them, respectively.

annotation = customer.Annotation();

Because XObject is the base class of every kind of X* class that is used to describe an XML
node, annotations can be added to any node. Usually, annotations are used to keep state
information, such as the mapping to source entities or documents used to build XML, while
the code handles real XML content.

No comments:

Post a Comment