455 lines
16 KiB
C#
455 lines
16 KiB
C#
using System.Globalization;
|
|
|
|
namespace Kit.Helpers
|
|
{
|
|
using System;
|
|
using System.Xml;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
|
|
public static class SystemXmlNodeExtensionMethods
|
|
{
|
|
private static readonly CultureInfo ci = new CultureInfo("en-US");
|
|
|
|
public static void RemoveAttribute(this XmlDocument input, string name)
|
|
{
|
|
foreach (XmlNode item in input.SelectNodes("//*[@{0}]".ApplyFormat(name)))
|
|
{
|
|
item.Attributes.Remove(item.Attributes[name]);
|
|
}
|
|
}
|
|
|
|
public static XmlNode SetElementValue(this XmlNode input,string name,string value)
|
|
{
|
|
var element = input.SelectSingleNode(name);
|
|
if (element == null)
|
|
{
|
|
element = input.OwnerDocument.CreateElement(name);
|
|
input.AppendChild(element);
|
|
}
|
|
element.InnerText = value;
|
|
return element;
|
|
}
|
|
|
|
public static XmlNode SetElement(this XmlNode input, string name, Object value)
|
|
{
|
|
var element = input.SelectSingleNode(name);
|
|
if (element == null)
|
|
{
|
|
element = input.OwnerDocument.CreateElement(name);
|
|
input.AppendChild(element);
|
|
}
|
|
element.InnerText = value.ToString();
|
|
return element;
|
|
}
|
|
|
|
|
|
public static XmlNode SetElementCDataValue(this XmlNode input, string name, string value)
|
|
{
|
|
var element = input.SelectSingleNode(name);
|
|
if (element == null)
|
|
{
|
|
var root = input.OwnerDocument.CreateElement(name);
|
|
element = input.OwnerDocument.CreateNode(XmlNodeType.CDATA, name, null);
|
|
root.AppendChild(element);
|
|
input.AppendChild(root);
|
|
}
|
|
element.InnerText = value;
|
|
return element;
|
|
}
|
|
|
|
|
|
public static XmlNode SetElementValue(this XmlNode input, string name, decimal value)
|
|
{
|
|
return input.SetElementValue(name, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
public static XmlNode SetElementValue(this XmlNode input, string name, decimal? value, string elementsAfterPostion)
|
|
{
|
|
if (value.HasValue)
|
|
{
|
|
return input.SetElementNullableValue(name, elementsAfterPostion, value.Value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
else
|
|
{
|
|
return input.SetElementNullableValue(name, null);
|
|
}
|
|
}
|
|
|
|
public static XmlNode SetElementValue(this XmlNode input, string name, int? value, string elementsAfterPostion)
|
|
{
|
|
if (value.HasValue && value != 0)
|
|
{
|
|
return input.SetElementNullableValue(name, elementsAfterPostion, value.Value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
else
|
|
{
|
|
return input.SetElementNullableValue(name, null);
|
|
}
|
|
}
|
|
|
|
private static XmlNode GetElementAfterInsert(XmlNode root, string elementsAfterPostion)
|
|
{
|
|
if (!elementsAfterPostion.IsNullOrEmpty())
|
|
{
|
|
var elements = root.SelectNodes(elementsAfterPostion);
|
|
return elements.Cast<XmlNode>().LastOrDefault();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static XmlNode SetElementNullableValue(this XmlNode input, string name, string value)
|
|
{
|
|
return input.SetElementNullableValue(name, "", value);
|
|
}
|
|
|
|
public static XmlNode SetElementNullableValue(this XmlNode input, string name, string elementsAfterPostion, string value)
|
|
{
|
|
if (!value.IsNullOrEmpty())
|
|
{
|
|
var element = input.SelectSingleNode(name);
|
|
if (element != null)
|
|
{
|
|
input.RemoveChild(element);
|
|
}
|
|
|
|
element = input.OwnerDocument.CreateElement(name);
|
|
var after = GetElementAfterInsert(input, elementsAfterPostion);
|
|
if (after == null)
|
|
input.InsertBefore(element, input.FirstChild);
|
|
else
|
|
input.InsertAfter(element, after);
|
|
|
|
element.InnerText = value;
|
|
|
|
return element;
|
|
}
|
|
else
|
|
{
|
|
var child = input.SelectSingleNode(name);
|
|
if (child != null)
|
|
input.RemoveChild(child);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static XmlNode CreateElement(this XmlNode input, string name)
|
|
{
|
|
//var element = input.SelectSingleNode(name);
|
|
//if (element == null)
|
|
//{
|
|
var element = input.OwnerDocument.CreateElement(name);
|
|
input.AppendChild(element);
|
|
//}
|
|
return element;
|
|
}
|
|
|
|
public static XmlAttribute CreateAttributeValue(this XmlNode input, string name, string value)
|
|
{
|
|
XmlAttribute element;
|
|
//try
|
|
{
|
|
element = input.Attributes[name];
|
|
}
|
|
//catch
|
|
{
|
|
|
|
if (element == null)
|
|
{
|
|
element = input.OwnerDocument.CreateAttribute(name);
|
|
input.Attributes.Append(element);
|
|
}
|
|
element.InnerText = value;
|
|
}
|
|
return element;
|
|
}
|
|
|
|
public static void SetAttribute(this XmlNode input, string name, Object value)
|
|
{
|
|
XmlAttribute element = null;
|
|
//try
|
|
{
|
|
element = input.Attributes[name];
|
|
}
|
|
//catch { }
|
|
if (element == null)
|
|
{
|
|
element = input.OwnerDocument.CreateAttribute(name);
|
|
input.Attributes.Append(element);
|
|
}
|
|
element.InnerText = value.ToString();
|
|
}
|
|
|
|
|
|
public static void SetAttributeValue(this XmlNode input, string name, string value)
|
|
{
|
|
XmlAttribute element = null;
|
|
//try
|
|
{
|
|
element = input.Attributes[name];
|
|
}
|
|
//catch { }
|
|
if (element == null)
|
|
{
|
|
element = input.OwnerDocument.CreateAttribute(name);
|
|
input.Attributes.Append(element);
|
|
}
|
|
element.InnerText = value;
|
|
}
|
|
|
|
public static void SetAttributeValue(this XmlNode input, string name, Guid? value)
|
|
{
|
|
XmlAttribute element = null;
|
|
//try
|
|
{
|
|
element = input.Attributes[name];
|
|
}
|
|
//catch { }
|
|
if (element == null)
|
|
{
|
|
element = input.OwnerDocument.CreateAttribute(name);
|
|
input.Attributes.Append(element);
|
|
}
|
|
element.InnerText = value.ToStringOrEmpty();
|
|
}
|
|
public static string GetValueAttribute(this XmlNode input, string name)
|
|
{
|
|
return input.GetValueAttribute(name, string.Empty);
|
|
}
|
|
|
|
public static string GetValueAttribute(this XmlNode input, string name, string empty)
|
|
{
|
|
XmlAttribute element = null;
|
|
//try
|
|
{
|
|
if (input == null) return empty;
|
|
element = input.Attributes[name];
|
|
|
|
if (element != null)
|
|
return element.InnerText;
|
|
else
|
|
return empty;
|
|
}
|
|
//catch
|
|
//{
|
|
// return empty;
|
|
//}
|
|
}
|
|
public static string GetValueAttribute(this XmlNode input, string name, string empty, XmlNamespaceManager namespaces)
|
|
{
|
|
XmlNode element = null;
|
|
//try
|
|
{
|
|
if (input == null) return empty;
|
|
element = input.SelectSingleNode(name, namespaces);
|
|
|
|
if (element != null)
|
|
return element.InnerText;
|
|
else
|
|
return empty;
|
|
}
|
|
//catch
|
|
//{
|
|
// return empty;
|
|
//}
|
|
}
|
|
public static string GetValue(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name,String.Empty);
|
|
}
|
|
|
|
public static string GetValue(this XmlNode input, string name, string empty)
|
|
{
|
|
var element = input.SelectSingleNode(name);
|
|
if (element == null) return empty;
|
|
return element.InnerText;
|
|
}
|
|
|
|
public static string GetValue(this XmlNode input, string name, XmlNamespaceManager namespaces)
|
|
{
|
|
return input.GetValue(name, String.Empty, namespaces);
|
|
}
|
|
|
|
public static string GetValue(this XmlNode input, string name, string empty, XmlNamespaceManager namespaces)
|
|
{
|
|
var element = input.SelectSingleNode(name, namespaces);
|
|
if (element == null) return empty;
|
|
return element.InnerText;
|
|
}
|
|
|
|
public static string GetValueCData(this XmlNode input, string name)
|
|
{
|
|
return input.GetValueCData(name, string.Empty);
|
|
}
|
|
|
|
public static string GetValueCData(this XmlNode input, string name, string empty)
|
|
{
|
|
var element = input.SelectSingleNode(name);
|
|
if (element == null) return empty;
|
|
if (element.FirstChild == null) return element.InnerText;
|
|
return element.FirstChild.InnerText;
|
|
}
|
|
|
|
public static DateTime? GetValueDate(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToDateXMLNullable();
|
|
}
|
|
|
|
public static DateTime GetValueDate(this XmlNode input, string name,DateTime empty)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToDateXMLNullable(empty);
|
|
}
|
|
|
|
public static short GetValueShort(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToShort();
|
|
}
|
|
|
|
public static bool GetValueBool(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToBool(false);
|
|
}
|
|
public static bool GetValueBool(this XmlNode input, string name, bool empty)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToBool(empty);
|
|
}
|
|
public static Int32? GetValueInt32Nulable(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToInt32Nulable();
|
|
}
|
|
|
|
public static Int32 GetValueInt32(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToInt32();
|
|
}
|
|
public static Int32 GetValueInt32(this XmlNode input, string name, int empty)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToInt32(empty);
|
|
}
|
|
public static byte GetValueByte(this XmlNode input, string name, byte empty)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToByte(empty);
|
|
}
|
|
public static decimal? GetValueDecimal(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToDecimalNulable();
|
|
}
|
|
public static decimal GetValueDecimal(this XmlNode input, string name, decimal empty)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToDecimal(empty);
|
|
}
|
|
public static Guid GetValueGuid(this XmlNode input, string name, Guid empty)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToGuid(empty);
|
|
}
|
|
|
|
public static Guid GetValueGuid(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToGuid(Guid.Empty);
|
|
}
|
|
public static Guid? GetValueGuidNullable(this XmlNode input, string name)
|
|
{
|
|
return input.GetValue(name, String.Empty).TryParseToGuidNulable(null);
|
|
}
|
|
|
|
public static Guid GetValueAttributeGuid(this XmlNode input, string name)
|
|
{
|
|
return input.GetValueAttributeGuid(name,null) ?? Guid.Empty;
|
|
}
|
|
|
|
public static Guid? GetValueAttributeGuid(this XmlNode input, string name, Guid? empty)
|
|
{
|
|
return input.GetValueAttribute(name, String.Empty).TryParseToGuidNulable(empty);
|
|
}
|
|
|
|
public static Guid? GetValueAttributeGuid(this XmlNode input, string name, XmlNamespaceManager namespaces, Guid? empty )
|
|
{
|
|
return input.GetValueAttribute(name, String.Empty).TryParseToGuidNulable(empty);
|
|
}
|
|
|
|
|
|
public static int GetValueAttributeInt32(this XmlNode input, string name)
|
|
{
|
|
return input.GetValueAttribute(name, String.Empty).TryParseToInt32(0);
|
|
}
|
|
|
|
public static int? GetValueAttributeInt32Nullable(this XmlNode input, string name)
|
|
{
|
|
return input.GetValueAttribute(name, String.Empty).TryParseToInt32Nulable();
|
|
}
|
|
|
|
public static bool GetValueAttributeBool(this XmlNode input, string name)
|
|
{
|
|
return input.GetValueAttribute(name, String.Empty).TryParseToBool(false);
|
|
}
|
|
public static bool GetValueAttributeBool(this XmlNode input, string name, bool empty)
|
|
{
|
|
return input.GetValueAttribute(name, String.Empty).TryParseToBool(empty);
|
|
}
|
|
|
|
|
|
public static string ToStringXMLShema(this DateTime dt)
|
|
{
|
|
if (dt == null) return String.Empty;
|
|
return dt.ToString("yyyy-MM-ddTHH:mm:ss");
|
|
}
|
|
|
|
public static string ToStringXMLShema(this DateTime? dt)
|
|
{
|
|
if (dt == null) return String.Empty;
|
|
return (dt ?? DateTime.UtcNow).ToString("yyyy-MM-ddTHH:mm:ss");
|
|
}
|
|
|
|
public static string ToStringSoutXML(this DateTime dt)
|
|
{
|
|
if (dt == null) return String.Empty;
|
|
return dt.ToString("yyyy-MM-dd");
|
|
}
|
|
|
|
public static string ToStringSoutXML(this DateTime? dt)
|
|
{
|
|
if (dt == null) return String.Empty;
|
|
return (dt??DateTime.UtcNow).ToString("yyyy-MM-dd");
|
|
}
|
|
|
|
public static double GetValueDouble_(this XmlNode input, string name)
|
|
{
|
|
double value;
|
|
if (double.TryParse(input.GetValue(name), NumberStyles.Any, ci.NumberFormat, out value))
|
|
return value;
|
|
return double.MinValue;
|
|
}
|
|
public static double GetValueDouble_(this XmlNode input)
|
|
{
|
|
double value;
|
|
if (double.TryParse(input.InnerText, NumberStyles.Any, ci.NumberFormat, out value))
|
|
return value;
|
|
return double.MinValue;
|
|
}
|
|
public static void ForEach(this XmlNodeList nodeList, Action<XmlNode> action)
|
|
{
|
|
foreach(XmlNode node in nodeList)
|
|
{
|
|
action(node);
|
|
}
|
|
}
|
|
public static IEnumerable<XmlNode> ToList<TResult>(this XmlNodeList nodeList)
|
|
{
|
|
var resultList = new List<XmlNode>();
|
|
foreach (XmlNode node in nodeList)
|
|
{
|
|
resultList.Add(node);
|
|
}
|
|
return resultList;
|
|
}
|
|
public static IEnumerable<TResult> Select<TResult>(this XmlNodeList nodeList, Func<XmlNode, TResult> action)
|
|
{
|
|
var resultList = new List<TResult>();
|
|
foreach (XmlNode node in nodeList)
|
|
{
|
|
resultList.Add(action(node));
|
|
}
|
|
return resultList;
|
|
}
|
|
}
|
|
} |