/// <summary> /// Deserialize a <see cref="XDocument"/> which includes integrated annotations into a new <see /// cref="XDocument"/> with applied annotations. /// </summary> /// <param name="document"></param> /// <returns></returns> public XDocument Deserialize(XDocument document) { Contract.Requires <ArgumentNullException>(document != null); // copy element and deserialize contents var xml = new XDocument(document); // document being read expresses a base URI, set on element if (!string.IsNullOrEmpty(document.BaseUri)) { xml.SetBaseUri(document.BaseUri); } // apply serialied contents DeserializeContents(xml); // strip out NX namespaces xml.Root.DescendantsAndSelf() .SelectMany(i => i.Attributes()) .Where(i => i.IsNamespaceDeclaration) .Where(i => i.Value == NX_NS) .Remove(); // strip out unsupported processing instructions xml.DescendantNodesAndSelf() .OfType <XProcessingInstruction>() .Where(i => i.NodeType != XmlNodeType.XmlDeclaration) .Remove(); return(xml); }
/// <summary> /// Disposes of the <see cref="Document"/>. /// </summary> public void Dispose() { GC.SuppressFinalize(this); if (xml != null) { // dispose of any annotations that support it var disposable = xml .DescendantNodesAndSelf() .SelectMany(i => i.Annotations <IDisposable>()); foreach (var dispose in disposable) { if (dispose != this) { dispose.Dispose(); } } } // dispose of host container if (container != null) { container.Dispose(); } }