using System;
using System.Xml;
using umbraco.IO;
namespace umbraco
{
///
/// The xmlHelper class contains general helper methods for working with xml in umbraco.
///
public class xmlHelper
{
///
/// Imports a XML node from text.
///
/// The text.
/// The XML doc.
///
public static XmlNode ImportXmlNodeFromText (string text, ref XmlDocument xmlDoc)
{
xmlDoc.LoadXml(text);
return xmlDoc.FirstChild;
}
///
/// Opens a file as a XmlDocument.
///
/// The relative file path. ei. /config/umbraco.config
/// Returns a XmlDocument class
public static XmlDocument OpenAsXmlDocument(string filePath) {
XmlTextReader reader = new XmlTextReader(IOHelper.MapPath(filePath));
reader.WhitespaceHandling = WhitespaceHandling.All;
XmlDocument xmlDoc = new XmlDocument();
//Load the file into the XmlDocument
xmlDoc.Load(reader);
//Close off the connection to the file.
reader.Close();
return xmlDoc;
}
///
/// creates a XmlAttribute with the specified name and value
///
/// The xmldocument.
/// The name of the attribute.
/// The value of the attribute.
/// a XmlAttribute
public static XmlAttribute addAttribute(XmlDocument Xd, string Name, string Value)
{
XmlAttribute temp = Xd.CreateAttribute(Name);
temp.Value = Value;
return temp;
}
///
/// Creates a text XmlNode with the specified name and value
///
/// The xmldocument.
/// The node name.
/// The node value.
/// a XmlNode
public static XmlNode addTextNode(XmlDocument Xd, string Name, string Value)
{
XmlNode temp = Xd.CreateNode(XmlNodeType.Element, Name, "");
temp.AppendChild(Xd.CreateTextNode(Value));
return temp;
}
///
/// Creates a cdata XmlNode with the specified name and value
///
/// The xmldocument.
/// The node name.
/// The node value.
/// A XmlNode
public static XmlNode addCDataNode(XmlDocument Xd, string Name, string Value)
{
XmlNode temp = Xd.CreateNode(XmlNodeType.Element, Name, "");
temp.AppendChild(Xd.CreateCDataSection(Value));
return temp;
}
///
/// Gets the value of a XmlNode
///
/// The XmlNode.
/// the value as a string
public static string GetNodeValue(XmlNode n)
{
if (n == null || n.FirstChild == null)
return string.Empty;
return n.FirstChild.Value;
}
}
}