2021-06-17 16:15:38 +10:00
|
|
|
// Copyright (c) Umbraco.
|
2021-02-18 11:06:02 +01:00
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using System.Xml.XPath;
|
2021-06-17 18:00:49 +10:00
|
|
|
using Umbraco.Cms.Core;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Xml;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
namespace Umbraco.Extensions;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extension methods for xml objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class XmlExtensions
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
public static bool HasAttribute(this XmlAttributeCollection attributes, string attributeName) =>
|
|
|
|
|
attributes.Cast<XmlAttribute>().Any(x => x.Name == attributeName);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects a list of XmlNode matching an XPath expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">A source XmlNode.</param>
|
|
|
|
|
/// <param name="expression">An XPath expression.</param>
|
|
|
|
|
/// <param name="variables">A set of XPathVariables.</param>
|
|
|
|
|
/// <returns>The list of XmlNode matching the XPath expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If
|
|
|
|
|
/// <param name="variables" />
|
|
|
|
|
/// is <c>null</c>, or is empty, or contains only one single
|
|
|
|
|
/// value which itself is <c>null</c>, then variables are ignored.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static XmlNodeList? SelectNodes(this XmlNode source, string expression, IEnumerable<XPathVariable>? variables)
|
|
|
|
|
{
|
|
|
|
|
XPathVariable[]? av = variables?.ToArray();
|
|
|
|
|
return SelectNodes(source, expression, av);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects a list of XmlNode matching an XPath expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">A source XmlNode.</param>
|
|
|
|
|
/// <param name="expression">An XPath expression.</param>
|
|
|
|
|
/// <param name="variables">A set of XPathVariables.</param>
|
|
|
|
|
/// <returns>The list of XmlNode matching the XPath expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If
|
|
|
|
|
/// <param name="variables" />
|
|
|
|
|
/// is <c>null</c>, or is empty, or contains only one single
|
|
|
|
|
/// value which itself is <c>null</c>, then variables are ignored.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static XmlNodeList? SelectNodes(this XmlNode source, XPathExpression expression, IEnumerable<XPathVariable>? variables)
|
|
|
|
|
{
|
|
|
|
|
XPathVariable[]? av = variables?.ToArray();
|
|
|
|
|
return SelectNodes(source, expression, av);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
/// <summary>
|
2022-06-07 15:28:38 +02:00
|
|
|
/// Selects a list of XmlNode matching an XPath expression.
|
2018-06-29 19:52:40 +02:00
|
|
|
/// </summary>
|
2022-06-07 15:28:38 +02:00
|
|
|
/// <param name="source">A source XmlNode.</param>
|
|
|
|
|
/// <param name="expression">An XPath expression.</param>
|
|
|
|
|
/// <param name="variables">A set of XPathVariables.</param>
|
|
|
|
|
/// <returns>The list of XmlNode matching the XPath expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If
|
|
|
|
|
/// <param name="variables" />
|
|
|
|
|
/// is <c>null</c>, or is empty, or contains only one single
|
|
|
|
|
/// value which itself is <c>null</c>, then variables are ignored.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static XmlNodeList? SelectNodes(this XmlNode source, string? expression, params XPathVariable[]? variables)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
if (variables == null || variables.Length == 0 || variables[0] == null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
return source.SelectNodes(expression ?? string.Empty);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
XPathNodeIterator? iterator = source.CreateNavigator()?.Select(expression ?? string.Empty, variables);
|
|
|
|
|
return XmlNodeListFactory.CreateNodeList(iterator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects a list of XmlNode matching an XPath expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">A source XmlNode.</param>
|
|
|
|
|
/// <param name="expression">An XPath expression.</param>
|
|
|
|
|
/// <param name="variables">A set of XPathVariables.</param>
|
|
|
|
|
/// <returns>The list of XmlNode matching the XPath expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If
|
|
|
|
|
/// <param name="variables" />
|
|
|
|
|
/// is <c>null</c>, or is empty, or contains only one single
|
|
|
|
|
/// value which itself is <c>null</c>, then variables are ignored.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static XmlNodeList SelectNodes(this XmlNode source, XPathExpression expression, params XPathVariable[]? variables)
|
|
|
|
|
{
|
|
|
|
|
if (variables == null || variables.Length == 0 || variables[0] == null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
return source.SelectNodes(expression);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
XPathNodeIterator? iterator = source.CreateNavigator()?.Select(expression, variables);
|
|
|
|
|
return XmlNodeListFactory.CreateNodeList(iterator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects the first XmlNode that matches an XPath expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">A source XmlNode.</param>
|
|
|
|
|
/// <param name="expression">An XPath expression.</param>
|
|
|
|
|
/// <param name="variables">A set of XPathVariables.</param>
|
|
|
|
|
/// <returns>The first XmlNode that matches the XPath expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If
|
|
|
|
|
/// <param name="variables" />
|
|
|
|
|
/// is <c>null</c>, or is empty, or contains only one single
|
|
|
|
|
/// value which itself is <c>null</c>, then variables are ignored.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static XmlNode? SelectSingleNode(this XmlNode source, string expression, IEnumerable<XPathVariable>? variables)
|
|
|
|
|
{
|
|
|
|
|
XPathVariable[]? av = variables?.ToArray();
|
|
|
|
|
return SelectSingleNode(source, expression, av);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects the first XmlNode that matches an XPath expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">A source XmlNode.</param>
|
|
|
|
|
/// <param name="expression">An XPath expression.</param>
|
|
|
|
|
/// <param name="variables">A set of XPathVariables.</param>
|
|
|
|
|
/// <returns>The first XmlNode that matches the XPath expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If
|
|
|
|
|
/// <param name="variables" />
|
|
|
|
|
/// is <c>null</c>, or is empty, or contains only one single
|
|
|
|
|
/// value which itself is <c>null</c>, then variables are ignored.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static XmlNode? SelectSingleNode(this XmlNode source, XPathExpression expression, IEnumerable<XPathVariable>? variables)
|
|
|
|
|
{
|
|
|
|
|
XPathVariable[]? av = variables?.ToArray();
|
|
|
|
|
return SelectSingleNode(source, expression, av);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Selects the first XmlNode that matches an XPath expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">A source XmlNode.</param>
|
|
|
|
|
/// <param name="expression">An XPath expression.</param>
|
|
|
|
|
/// <param name="variables">A set of XPathVariables.</param>
|
|
|
|
|
/// <returns>The first XmlNode that matches the XPath expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If
|
|
|
|
|
/// <param name="variables" />
|
|
|
|
|
/// is <c>null</c>, or is empty, or contains only one single
|
|
|
|
|
/// value which itself is <c>null</c>, then variables are ignored.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static XmlNode? SelectSingleNode(this XmlNode source, string expression, params XPathVariable[]? variables)
|
|
|
|
|
{
|
|
|
|
|
if (variables == null || variables.Length == 0 || variables[0] == null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
return source.SelectSingleNode(expression);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
return SelectNodes(source, expression, variables)?.Cast<XmlNode>().FirstOrDefault();
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Selects the first XmlNode that matches an XPath expression.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">A source XmlNode.</param>
|
|
|
|
|
/// <param name="expression">An XPath expression.</param>
|
|
|
|
|
/// <param name="variables">A set of XPathVariables.</param>
|
|
|
|
|
/// <returns>The first XmlNode that matches the XPath expression.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If
|
|
|
|
|
/// <param name="variables" />
|
|
|
|
|
/// is <c>null</c>, or is empty, or contains only one single
|
|
|
|
|
/// value which itself is <c>null</c>, then variables are ignored.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static XmlNode? SelectSingleNode(this XmlNode source, XPathExpression expression, params XPathVariable[]? variables)
|
|
|
|
|
{
|
|
|
|
|
if (variables == null || variables.Length == 0 || variables[0] == null)
|
|
|
|
|
{
|
|
|
|
|
return source.SelectSingleNode(expression);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
return SelectNodes(source, expression, variables).Cast<XmlNode>().FirstOrDefault();
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Converts from an XDocument to an XmlDocument
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="xDocument"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static XmlDocument ToXmlDocument(this XDocument xDocument)
|
|
|
|
|
{
|
|
|
|
|
var xmlDocument = new XmlDocument();
|
|
|
|
|
using (XmlReader xmlReader = xDocument.CreateReader())
|
|
|
|
|
{
|
|
|
|
|
xmlDocument.Load(xmlReader);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
return xmlDocument;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts from an XmlDocument to an XDocument
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="xmlDocument"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static XDocument ToXDocument(this XmlDocument xmlDocument)
|
|
|
|
|
{
|
|
|
|
|
using (var nodeReader = new XmlNodeReader(xmlDocument))
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
nodeReader.MoveToContent();
|
|
|
|
|
return XDocument.Load(nodeReader);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2022-06-07 15:28:38 +02:00
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
///// <summary>
|
|
|
|
|
///// Converts from an XElement to an XmlElement
|
|
|
|
|
///// </summary>
|
|
|
|
|
///// <param name="xElement"></param>
|
|
|
|
|
///// <returns></returns>
|
|
|
|
|
public static XmlNode? ToXmlElement(this XContainer xElement)
|
|
|
|
|
{
|
|
|
|
|
var xmlDocument = new XmlDocument();
|
|
|
|
|
using (XmlReader xmlReader = xElement.CreateReader())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
xmlDocument.Load(xmlReader);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
return xmlDocument.DocumentElement;
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Converts from an XmlElement to an XElement
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="xmlElement"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static XElement ToXElement(this XmlNode xmlElement)
|
|
|
|
|
{
|
|
|
|
|
using (var nodeReader = new XmlNodeReader(xmlElement))
|
|
|
|
|
{
|
|
|
|
|
nodeReader.MoveToContent();
|
|
|
|
|
return XElement.Load(nodeReader);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2022-06-07 15:28:38 +02:00
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
public static T? RequiredAttributeValue<T>(this XElement xml, string attributeName)
|
|
|
|
|
{
|
|
|
|
|
if (xml == null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
throw new ArgumentNullException(nameof(xml));
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
if (xml.HasAttributes == false)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
throw new InvalidOperationException($"{attributeName} not found in xml");
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
XAttribute? attribute = xml.Attribute(attributeName);
|
|
|
|
|
if (attribute is null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
throw new InvalidOperationException($"{attributeName} not found in xml");
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
Attempt<T> result = attribute.Value.TryConvertTo<T>();
|
|
|
|
|
if (result.Success)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
return result.Result;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
throw new InvalidOperationException($"{attribute.Value} attribute value cannot be converted to {typeof(T)}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T? AttributeValue<T>(this XElement xml, string attributeName)
|
|
|
|
|
{
|
|
|
|
|
if (xml == null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
throw new ArgumentNullException("xml");
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
if (xml.HasAttributes == false)
|
2021-06-17 16:15:38 +10:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
return default;
|
2021-06-17 16:15:38 +10:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
if (xml.Attribute(attributeName) == null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
return default;
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
var val = xml.Attribute(attributeName)?.Value;
|
|
|
|
|
Attempt<T> result = val.TryConvertTo<T>();
|
|
|
|
|
if (result.Success)
|
|
|
|
|
{
|
|
|
|
|
return result.Result;
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
return default;
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
public static T? AttributeValue<T>(this XmlNode xml, string attributeName)
|
|
|
|
|
{
|
|
|
|
|
if (xml == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("xml");
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2021-06-17 16:15:38 +10:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
if (xml.Attributes == null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
return default;
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
if (xml.Attributes[attributeName] == null)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
var val = xml.Attributes[attributeName]?.Value;
|
|
|
|
|
Attempt<T> result = val.TryConvertTo<T>();
|
|
|
|
|
if (result.Success)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
return result.Result;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static XElement? GetXElement(this XmlNode node)
|
|
|
|
|
{
|
|
|
|
|
var xDoc = new XDocument();
|
|
|
|
|
using (XmlWriter xmlWriter = xDoc.CreateWriter())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
node.WriteTo(xmlWriter);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
return xDoc.Root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static XmlNode? GetXmlNode(this XContainer element)
|
|
|
|
|
{
|
|
|
|
|
using (XmlReader xmlReader = element.CreateReader())
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
var xmlDoc = new XmlDocument();
|
|
|
|
|
xmlDoc.Load(xmlReader);
|
|
|
|
|
return xmlDoc.DocumentElement;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-11 11:47:31 +01:00
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
public static XmlNode? GetXmlNode(this XContainer element, XmlDocument xmlDoc)
|
|
|
|
|
{
|
|
|
|
|
XmlNode? node = element.GetXmlNode();
|
|
|
|
|
if (node is not null)
|
|
|
|
|
{
|
|
|
|
|
return xmlDoc.ImportNode(node, true);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-07 15:28:38 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this exists because
|
|
|
|
|
// new XElement("root", "a\nb").Value is "a\nb" but
|
|
|
|
|
// .ToString(SaveOptions.*) is "a\r\nb" and cannot figure out how to get rid of "\r"
|
|
|
|
|
// and when saving data we want nothing to change
|
|
|
|
|
// this method will produce a string that respects the \r and \n in the data value
|
|
|
|
|
public static string ToDataString(this XElement xml)
|
|
|
|
|
{
|
|
|
|
|
var settings = new XmlWriterSettings
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2022-06-07 15:28:38 +02:00
|
|
|
OmitXmlDeclaration = true,
|
|
|
|
|
NewLineHandling = NewLineHandling.None,
|
|
|
|
|
Indent = false,
|
|
|
|
|
};
|
|
|
|
|
var output = new StringBuilder();
|
|
|
|
|
using (var writer = XmlWriter.Create(output, settings))
|
|
|
|
|
{
|
|
|
|
|
xml.WriteTo(writer);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2022-06-07 15:28:38 +02:00
|
|
|
|
|
|
|
|
return output.ToString();
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
}
|