/// <inheritdoc/> public IElementSnapshot CreateSnapshot(DependencyObject element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } var childCount = VisualTreeHelper.GetChildrenCount(element); var propertyValues = new object[PropertyNames.Count]; for (int i = 0; i < PropertyNames.Count; i++) { if (element?.GetType().GetProperty(PropertyNames[i]) is PropertyInfo propertyInfo) { propertyValues[i] = propertyInfo.GetValue(element); } } var childrenSnapshots = new IElementSnapshot[childCount]; for (int i = 0; i < childCount; i++) { childrenSnapshots[i] = CreateSnapshot(VisualTreeHelper.GetChild(element, i)); } return(new ElementSnapshot(childrenSnapshots, element.GetType().FullName, element is FrameworkElement fwElement ? fwElement.Name : "Unknown", PropertyNames, propertyValues)); }
/// <summary> /// Creates an <see cref="XmlDocument"/> representing the provided snapshot. /// </summary> /// <param name="snapshot">The snapshot to convert to XML.</param> /// <param name="indluceNullAndEmptyValues">Indicates whether null values should be included in the export or not. Set to true to include null values.</param> /// <param name="includeNameSpaces">Determines whether the full namespace of an element will be included or not. Set to false to only include the type name but not the namespace.</param> /// <returns>The <see cref="XmlDocument"/> representation of the object.</returns> public XmlDocument CreateXMLDocument(IElementSnapshot snapshot, bool indluceNullAndEmptyValues, bool includeNameSpaces) { if (snapshot is null) { throw new ArgumentNullException(nameof(snapshot)); } var xmlDocument = new XmlDocument(); xmlDocument.AppendChild(CreateXMLElement(xmlDocument, snapshot, indluceNullAndEmptyValues, includeNameSpaces)); return(xmlDocument); }
/// <summary> /// Creates an <see cref="XmlDocument"/> representing the provided snapshot. /// </summary> /// <param name="snapshot">The snapshot to convert to XML.</param> /// <param name="indluceNullAndEmptyValues">Indicates whether null values should be included in the export or not. Set to true to include null values.</param> /// <param name="includeNameSpaces">Determines whether the full namespace of an element will be included or not. Set to false to only include the type name but not the namespace.</param> /// <returns>The <see cref="XmlDocument"/> representation of the object.</returns> public JsonObject CreateJsonObject(IElementSnapshot snapshot, bool indluceNullAndEmptyValues, bool includeNameSpaces) { if (snapshot is null) { throw new ArgumentNullException(nameof(snapshot)); } JsonObject jsonObject = new JsonObject(); if (includeNameSpaces) { jsonObject.Add("type", JsonValue.CreateStringValue(snapshot.FullTypeName)); } else { jsonObject.Add("type", JsonValue.CreateStringValue(NamespaceHelper.GetTypeName(snapshot.FullTypeName))); } // The XML export should be somewhat usable as XAML. // Because of that, the export will strip the Windows.UI.Xaml.Controls namespace. for (int i = 0; i < snapshot.PropertyNames.Count; i++) { if (indluceNullAndEmptyValues || !(snapshot.PropertyValues[i] is null)) { var name = snapshot.PropertyNames[i]; string stringValue; if (ObjectToStringConverter is null) { stringValue = snapshot.PropertyValues[i]?.ToString() ?? "[null]"; } else { stringValue = ObjectToStringConverter.GetStringRepresentation(snapshot.PropertyValues[i]) ?? "[null]"; } if (indluceNullAndEmptyValues || !string.IsNullOrEmpty(stringValue)) { jsonObject.Add(name, JsonValue.CreateStringValue(stringValue)); } } } var jsonArray = new JsonArray(); for (int i = 0; i < snapshot.Children.Count; i++) { jsonArray.Add(CreateJsonObject(snapshot.Children[i], indluceNullAndEmptyValues, includeNameSpaces)); } jsonObject.Add("children", jsonArray); return(jsonObject); }
private XmlElement CreateXMLElement(XmlDocument document, IElementSnapshot snapshot, bool indluceNullAndEmptyValues, bool includeNameSpaces) { XmlElement xmlElement; if (includeNameSpaces) { xmlElement = document.CreateElement(snapshot.FullTypeName); } else { xmlElement = document.CreateElement(NamespaceHelper.GetTypeName(snapshot.FullTypeName)); } // The XML export should be somewhat usable as XAML. // Because of that, the export will strip the Windows.UI.Xaml.Controls namespace. for (int i = 0; i < snapshot.PropertyNames.Count; i++) { if (indluceNullAndEmptyValues || !(snapshot.PropertyValues[i] is null)) { var attributeNode = document.CreateAttribute(snapshot.PropertyNames[i]); if (ObjectToStringConverter is null) { attributeNode.Value = snapshot.PropertyValues[i]?.ToString() ?? "[null]"; } else { attributeNode.Value = ObjectToStringConverter.GetStringRepresentation(snapshot.PropertyValues[i]) ?? "[null]"; } if (indluceNullAndEmptyValues || !string.IsNullOrEmpty(attributeNode.Value)) { xmlElement.Attributes.SetNamedItem(attributeNode); } } } for (int i = 0; i < snapshot.Children.Count; i++) { xmlElement.AppendChild(CreateXMLElement(document, snapshot.Children[i], indluceNullAndEmptyValues, includeNameSpaces)); } return(xmlElement); }
/// <summary> /// Creates an <see cref="XmlDocument"/> representing the provided snapshot. /// </summary> /// <param name="snapshot">The snapshot to convert to a formatted XML string.</param> /// <param name="indluceNullAndEmptyValues">Indicates whether null values should be included in the export or not. Set to true to include null values.</param> /// <param name="includeNameSpaces">Determines whether the full namespace of an element will be included or not. Set to false to only include the type name but not the namespace.</param> /// <returns>The formatted XML string representation.</returns> public string CreateFormattedXMLString(IElementSnapshot snapshot, bool indluceNullAndEmptyValues, bool includeNameSpaces) { var rawXML = CreateXMLDocument(snapshot, indluceNullAndEmptyValues, includeNameSpaces).GetXml(); return(XDocument.Parse(rawXML).ToString()); }
/// <summary> /// Creates an <see cref="XmlDocument"/> representing the provided snapshot. /// </summary> /// <param name="snapshot">The snapshot to convert to a formatted XML string.</param> /// <param name="indluceNullAndEmptyValues">Indicates whether null values should be included in the export or not. Set to true to include null values.</param> /// <param name="includeNameSpaces">Determines whether the full namespace of an element will be included or not. Set to false to only include the type name but not the namespace.</param> /// <returns>The formatted XML string representation.</returns> public string CreateFormattedJSONString(IElementSnapshot snapshot, bool indluceNullAndEmptyValues, bool includeNameSpaces) { var rawJSON = CreateJsonObject(snapshot, indluceNullAndEmptyValues, includeNameSpaces).Stringify(); return(JsonHelper.FormatJson(rawJSON)); }