/// <summary> /// Performs shared initialization between Load and LoadAsync. /// </summary> static XDocument InitLoad(XmlReader reader, LoadOptions options) { XDocument d = new XDocument(); if ((options & LoadOptions.SetBaseUri) != 0) { string baseUri = reader.BaseURI; if (!string.IsNullOrEmpty(baseUri)) { d.SetBaseUri(baseUri); } } if ((options & LoadOptions.SetLineInfo) != 0) { IXmlLineInfo li = reader as IXmlLineInfo; if (li != null && li.HasLineInfo()) { d.SetLineInfo(li.LineNumber, li.LinePosition); } } if (reader.NodeType == XmlNodeType.XmlDeclaration) { d.Declaration = new XDeclaration(reader); } return(d); }
/// <summary> /// Create a new <see cref="XDocument"/> containing the contents of the /// passed in <see cref="XmlReader"/>. /// </summary> /// <param name="reader"> /// An <see cref="XmlReader"/> containing the XML to be read into the new /// <see cref="XDocument"/>. /// </param> /// <param name="options"> /// A set of <see cref="LoadOptions"/>. /// </param> /// <returns> /// A new <see cref="XDocument"/> containing the contents of the passed /// in <see cref="XmlReader"/>. /// </returns> public static XDocument Load(XmlReader reader, LoadOptions options) { if (reader == null) { throw new ArgumentNullException("reader"); } if (reader.ReadState == ReadState.Initial) { reader.Read(); } XDocument d = new XDocument(); if ((options & LoadOptions.SetBaseUri) != 0) { string baseUri = reader.BaseURI; if (baseUri != null && baseUri.Length != 0) { d.SetBaseUri(baseUri); } } if ((options & LoadOptions.SetLineInfo) != 0) { IXmlLineInfo li = reader as IXmlLineInfo; if (li != null && li.HasLineInfo()) { d.SetLineInfo(li.LineNumber, li.LinePosition); } } if (reader.NodeType == XmlNodeType.XmlDeclaration) { d.Declaration = new XDeclaration(reader); } d.ReadContentFrom(reader, options); if (!reader.EOF) { throw new InvalidOperationException(SR.InvalidOperation_ExpectedEndOfFile); } if (d.Root == null) { throw new InvalidOperationException(SR.InvalidOperation_MissingRoot); } return(d); }
public static XDocument Load(XmlReader reader, LoadOptions options) { if (reader == null) { throw new ArgumentNullException("reader"); } if (reader.ReadState == System.Xml.ReadState.Initial) { reader.Read(); } XDocument document = new XDocument(); if ((options & LoadOptions.SetBaseUri) != LoadOptions.None) { string baseURI = reader.BaseURI; if ((baseURI != null) && (baseURI.Length != 0)) { document.SetBaseUri(baseURI); } } if ((options & LoadOptions.SetLineInfo) != LoadOptions.None) { IXmlLineInfo info = reader as IXmlLineInfo; if ((info != null) && info.HasLineInfo()) { document.SetLineInfo(info.LineNumber, info.LinePosition); } } if (reader.NodeType == XmlNodeType.XmlDeclaration) { document.Declaration = new XDeclaration(reader); } document.ReadContentFrom(reader, options); if (!reader.EOF) { throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_ExpectedEndOfFile")); } if (document.Root == null) { throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_MissingRoot")); } return(document); }
/// <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; }