This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathHelper-Xml.cs
More file actions
86 lines (78 loc) · 2.56 KB
/
Copy pathHelper-Xml.cs
File metadata and controls
86 lines (78 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Xml;
using Umbraco.Core;
namespace uComponents.Core
{
/// <summary>
/// Generic helper methods
/// </summary>
internal static partial class Helper
{
/// <summary>
/// Xml helper
/// </summary>
public static class Xml
{
/// <summary>
/// Determines whether the specified string appears to be XML.
/// </summary>
/// <param name="xml">The XML string.</param>
/// <returns>
/// <c>true</c> if the specified string appears to be XML; otherwise, <c>false</c>.
/// </returns>
public static bool CouldItBeXml(string xml)
{
if (!string.IsNullOrEmpty(xml))
{
xml = xml.Trim();
if (xml.StartsWith("<") && xml.EndsWith(">"))
{
return true;
}
}
return false;
}
/// <summary>
/// Splits the specified delimited string into an XML document.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="separator">The separator.</param>
/// <param name="rootName">Name of the root.</param>
/// <param name="elementName">Name of the element.</param>
/// <returns>Returns an <c>System.Xml.XmlDocument</c> representation of the delimited string data.</returns>
public static XmlDocument Split(string data, string[] separator, string rootName, string elementName)
{
return Split(new XmlDocument(), data, separator, rootName, elementName);
}
/// <summary>
/// Splits the specified delimited string into an XML document.
/// </summary>
/// <param name="xml">The XML document.</param>
/// <param name="data">The delimited string data.</param>
/// <param name="separator">The separator.</param>
/// <param name="rootName">Name of the root node.</param>
/// <param name="elementName">Name of the element node.</param>
/// <returns>Returns an <c>System.Xml.XmlDocument</c> representation of the delimited string data.</returns>
public static XmlDocument Split(XmlDocument xml, string data, string[] separator, string rootName, string elementName)
{
// load new XML document.
xml.LoadXml(string.Concat("<", rootName, "/>"));
// get the data-value, check it isn't empty.
if (!string.IsNullOrEmpty(data))
{
// explode the values into an array
var values = data.Split(separator, StringSplitOptions.None);
// loop through the array items.
foreach (string value in values)
{
// add each value to the XML document.
var xn = XmlHelper.AddTextNode(xml, elementName, value);
xml.DocumentElement.AppendChild(xn);
}
}
// return the XML node.
return xml;
}
}
}
}