55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Umbraco.Core
|
|
{
|
|
/// <summary>
|
|
/// Extension methods for xml objects
|
|
/// </summary>
|
|
internal static class XmlExtensions
|
|
{
|
|
public static T AttributeValue<T>(this XmlNode xml, string attributeName)
|
|
{
|
|
if (xml == null) throw new ArgumentNullException("xml");
|
|
if (xml.Attributes == null) return default(T);
|
|
|
|
if (xml.Attributes[attributeName] == null)
|
|
return default(T);
|
|
|
|
var val = xml.Attributes[attributeName].Value;
|
|
var result = val.TryConvertTo<T>();
|
|
if (result.Success)
|
|
return result.Result;
|
|
|
|
return default(T);
|
|
}
|
|
|
|
public static XElement GetXElement(this XmlNode node)
|
|
{
|
|
XDocument xDoc = new XDocument();
|
|
using (XmlWriter xmlWriter = xDoc.CreateWriter())
|
|
node.WriteTo(xmlWriter);
|
|
return xDoc.Root;
|
|
}
|
|
|
|
public static XmlNode GetXmlNode(this XElement element)
|
|
{
|
|
using (XmlReader xmlReader = element.CreateReader())
|
|
{
|
|
XmlDocument xmlDoc = new XmlDocument();
|
|
xmlDoc.Load(xmlReader);
|
|
return xmlDoc.FirstChild;
|
|
}
|
|
}
|
|
|
|
public static XmlNode GetXmlNode(this XElement element, XmlDocument xmlDoc)
|
|
{
|
|
using (XmlReader xmlReader = element.CreateReader())
|
|
{
|
|
xmlDoc.Load(xmlReader);
|
|
return xmlDoc.DocumentElement;
|
|
}
|
|
}
|
|
}
|
|
} |