/// <summary> /// Get RootElement of the current part. /// </summary> /// <param name="part">Current Part</param> /// <returns>Return Root element of the pass-in part</returns> public static OpenXmlPartRootElement RootElement(this OpenXmlPart part) { if (null == part) { throw new ArgumentNullException(nameof(part)); } if (part is CustomXmlPart) { XmlDocument xmlDoc = new XmlDocument(); using (var stream = part.GetStream()) xmlDoc.Load(stream); if (part.IsBibliographyPart()) { return(new Sources(xmlDoc.DocumentElement.OuterXml)); } else if (part.IsInkPart()) { return(new DocumentFormat.OpenXml.InkML.Ink(xmlDoc.DocumentElement.OuterXml)); } } var flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy; var properties = part.GetType().GetProperties(flag) .Where(p => p.PropertyType.IsSubclassOf(typeof(OpenXmlPartRootElement))); return(null == properties.FirstOrDefault() ? null : properties.First().GetValue(part, null) as OpenXmlPartRootElement); }
/// <summary> /// Get RootElement of the current part. /// </summary> /// <param name="part">Current Part</param> /// <returns>Return Root element of the pass-in part</returns> public static OpenXmlPartRootElement RootElement(this OpenXmlPart part) { if (null == part) { throw new ArgumentNullException(nameof(part)); } if (part is CustomXmlPart) { using (var stream = part.GetStream()) { var xmlDoc = new XmlDocument(); xmlDoc.Load(stream); if (part.IsBibliographyPart()) { return(new Sources(xmlDoc.DocumentElement.OuterXml)); } else if (part.IsInkPart()) { return(new InkML.Ink(xmlDoc.DocumentElement.OuterXml)); } } } var flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy; var property = part.GetType().GetProperties(flag) .Where(p => p.PropertyType.IsSubclassOf(typeof(OpenXmlPartRootElement))) .FirstOrDefault(); if (property == null) { return(null); } try { return(property.GetValue(part, null) as OpenXmlPartRootElement); } catch (TargetInvocationException e) { throw e.InnerException; } }
/// <summary> /// Check if current part is reflectable (with xml content). /// </summary> /// <param name="part">Current part</param> /// <returns>return true if it has non-null root element, otherwise rturn false.</returns> public static bool IsReflectable(this OpenXmlPart part) { if (null == part) { throw new ArgumentNullException(nameof(part)); } if (part.IsBibliographyPart() || part.IsAdditionalCharacteristicsPart() || part.IsInkPart()) { return(true); } var flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy; var property = part.GetType().GetProperties(flag) .Where(p => p.PropertyType.IsSubclassOf(typeof(OpenXmlPartRootElement))) .FirstOrDefault(); return(null != property); }