Core.Xml - refactor
This commit is contained in:
@@ -765,6 +765,7 @@
|
||||
<Compile Include="Xml\XmlNamespaces.cs" />
|
||||
<Compile Include="Xml\XmlNodeListFactory.cs" />
|
||||
<Compile Include="Xml\XmlNodeExtensions.cs" />
|
||||
<Compile Include="Xml\XPathNavigatorExtensions.cs" />
|
||||
<Compile Include="Xml\XPathVariable.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
30
src/Umbraco.Core/Xml/XPathNavigatorExtensions.cs
Normal file
30
src/Umbraco.Core/Xml/XPathNavigatorExtensions.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Xml.XPath;
|
||||
|
||||
namespace Umbraco.Core.Xml
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extensions to XPathNavigator.
|
||||
/// </summary>
|
||||
internal static class XPathNavigatorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Selects a node set, using the specified XPath expression.
|
||||
/// </summary>
|
||||
/// <param name="navigator">A source XPathNavigator.</param>
|
||||
/// <param name="expression">An XPath expression.</param>
|
||||
/// <param name="variables">A set of XPathVariables.</param>
|
||||
/// <returns>An iterator over the nodes matching the specified expression.</returns>
|
||||
public static XPathNodeIterator Select(this XPathNavigator navigator, string expression, params XPathVariable[] variables)
|
||||
{
|
||||
if (variables == null || variables.Length == 0 || variables[0] == null)
|
||||
return navigator.Select(expression);
|
||||
|
||||
var compiled = navigator.Compile(expression);
|
||||
var context = new DynamicContext();
|
||||
foreach (var variable in variables)
|
||||
context.AddVariable(variable.Name, variable.Value);
|
||||
compiled.SetContext(context);
|
||||
return navigator.Select(compiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using System.Xml.XPath;
|
||||
|
||||
// source: mvpxml.codeplex.com
|
||||
|
||||
namespace Umbraco.Core.Xml
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extensions to XmlNode.
|
||||
/// </summary>
|
||||
internal static class XmlNodeExtensions
|
||||
{
|
||||
static XPathNodeIterator Select(string expression, XPathNavigator source, params XPathVariable[] variables)
|
||||
{
|
||||
var expr = source.Compile(expression);
|
||||
var context = new DynamicContext();
|
||||
foreach (var variable in variables)
|
||||
context.AddVariable(variable.Name, variable.Value);
|
||||
expr.SetContext(context);
|
||||
return source.Select(expr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects a list of XmlNode matching an XPath expression.
|
||||
/// </summary>
|
||||
@@ -54,7 +46,7 @@ namespace Umbraco.Core.Xml
|
||||
if (variables == null || variables.Length == 0 || variables[0] == null)
|
||||
return source.SelectNodes(expression);
|
||||
|
||||
var iterator = Select(expression, source.CreateNavigator(), variables);
|
||||
var iterator = source.CreateNavigator().Select(expression, variables);
|
||||
return XmlNodeListFactory.CreateNodeList(iterator);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
using System.Xml.XPath;
|
||||
using Umbraco.Core.IO;
|
||||
|
||||
namespace Umbraco.Core
|
||||
@@ -14,6 +15,28 @@ namespace Umbraco.Core
|
||||
/// </summary>
|
||||
public class XmlHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether a specified string contains only xml whitespace characters.
|
||||
/// </summary>
|
||||
/// <param name="s">The string.</param>
|
||||
/// <returns><c>true</c> if the string contains only xml whitespace characters.</returns>
|
||||
/// <remarks>As per XML 1.1 specs, space, \t, \r and \n.</remarks>
|
||||
public static bool IsXmlWhitespace(string s)
|
||||
{
|
||||
// as per xml 1.1 specs - anything else is significant whitespace
|
||||
s = s.Trim(' ', '\t', '\r', '\n');
|
||||
return s.Length == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <c>XPathDocument</c> from an xml string.
|
||||
/// </summary>
|
||||
/// <param name="xml">The xml string.</param>
|
||||
/// <returns>An <c>XPathDocument</c> created from the xml string.</returns>
|
||||
public static XPathDocument CreateXPathDocument(string xml)
|
||||
{
|
||||
return new XPathDocument(new XmlTextReader(new StringReader(xml)));
|
||||
}
|
||||
|
||||
public static string StripDashesInElementOrAttributeNames(string xml)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user