Class XmlHelper
The XmlHelper class contains general helper methods for working with xml in umbraco.
Inheritance
Namespace: Umbraco.Cms.Core.Xml
Assembly: Umbraco.Core.dll
Syntax
public class XmlHelper
  Methods
View SourceAddAttribute(XmlDocument, String, String)
creates a XmlAttribute with the specified name and value
Declaration
public static XmlAttribute AddAttribute(XmlDocument xd, string name, string value)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlDocument | xd | The xmldocument.  | 
      
| System.String | name | The name of the attribute.  | 
      
| System.String | value | The value of the attribute.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Xml.XmlAttribute | a XmlAttribute  | 
      
AddCDataNode(XmlDocument, String, String)
Creates a cdata XmlNode with the specified name and value
Declaration
public static XmlNode AddCDataNode(XmlDocument xd, string name, string value)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlDocument | xd | The xmldocument.  | 
      
| System.String | name | The node name.  | 
      
| System.String | value | The node value.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Xml.XmlNode | A XmlNode  | 
      
AddTextNode(XmlDocument, String, String)
Creates a text XmlNode with the specified name and value
Declaration
public static XmlNode AddTextNode(XmlDocument xd, string name, string value)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlDocument | xd | The xmldocument.  | 
      
| System.String | name | The node name.  | 
      
| System.String | value | The node value.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Xml.XmlNode | a XmlNode  | 
      
CouldItBeXml(String)
Determines whether the specified string appears to be XML.
Declaration
public static bool CouldItBeXml(string xml)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.String | xml | The XML string.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Boolean | 
  | 
      
CreateXPathDocument(String)
Creates a new XPathDocument from an xml string.
Declaration
public static XPathDocument CreateXPathDocument(string xml)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.String | xml | The xml string.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Xml.XPath.XPathDocument | An   | 
      
GetAttributesFromElement(String)
Return a dictionary of attributes found for a string based tag
Declaration
public static Dictionary<string, string> GetAttributesFromElement(string tag)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.String | tag | 
Returns
| Type | Description | 
|---|---|
| Umbraco.Cms.Core.Dictionary<System.String, System.String> | 
GetNodeValue(XmlNode)
Gets the value of a XmlNode
Declaration
public static string GetNodeValue(XmlNode n)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlNode | n | The XmlNode.  | 
      
Returns
| Type | Description | 
|---|---|
| System.String | the value as a string  | 
      
IsXmlWhitespace(String)
Gets a value indicating whether a specified string contains only xml whitespace characters.
Declaration
public static bool IsXmlWhitespace(string s)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.String | s | The string.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Boolean | 
  | 
      
Remarks
As per XML 1.1 specs, space, \t, \r and \n.
OpenAsXmlDocument(String, IHostingEnvironment)
Opens a file as a XmlDocument.
Declaration
public static XmlDocument OpenAsXmlDocument(string filePath, IHostingEnvironment hostingEnvironment)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.String | filePath | The relative file path. ie. /config/umbraco.config  | 
      
| IHostingEnvironment | hostingEnvironment | 
Returns
| Type | Description | 
|---|---|
| System.Xml.XmlDocument | Returns a XmlDocument class  | 
      
SetAttribute(XmlDocument, XmlNode, String, String)
Creates or sets an attribute on the XmlNode if an Attributes collection is available
Declaration
public static void SetAttribute(XmlDocument xml, XmlNode n, string name, string value)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlDocument | xml | |
| System.Xml.XmlNode | n | |
| System.String | name | |
| System.String | value | 
SetCDataNode(XmlDocument, XmlNode, String, String)
Sets or Creates a cdata XmlNode with the specified name and value
Declaration
public static XmlNode SetCDataNode(XmlDocument xd, XmlNode parent, string name, string value)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlDocument | xd | The xmldocument.  | 
      
| System.Xml.XmlNode | parent | The node to set or create the child text node on  | 
      
| System.String | name | The node name.  | 
      
| System.String | value | The node value.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Xml.XmlNode | a XmlNode  | 
      
SetInnerXmlNode(XmlDocument, XmlNode, String, String)
Sets or creates an Xml node from its inner Xml.
Declaration
public static XmlNode SetInnerXmlNode(XmlDocument xd, XmlNode parent, string name, string value)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlDocument | xd | The xmldocument.  | 
      
| System.Xml.XmlNode | parent | The node to set or create the child text node on  | 
      
| System.String | name | The node name.  | 
      
| System.String | value | The node inner Xml.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Xml.XmlNode | a XmlNode  | 
      
SetTextNode(XmlDocument, XmlNode, String, String)
Sets or Creates a text XmlNode with the specified name and value
Declaration
public static XmlNode SetTextNode(XmlDocument xd, XmlNode parent, string name, string value)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlDocument | xd | The xmldocument.  | 
      
| System.Xml.XmlNode | parent | The node to set or create the child text node on  | 
      
| System.String | name | The node name.  | 
      
| System.String | value | The node value.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Xml.XmlNode | a XmlNode  | 
      
SortNode(XmlNode, String, XmlNode, Func<XmlNode, Int32>)
Sorts a single child node of a parentNode.
Declaration
public static bool SortNode(XmlNode parentNode, string childNodesXPath, XmlNode node, Func<XmlNode, int> orderBy)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlNode | parentNode | The parent node.  | 
      
| System.String | childNodesXPath | An XPath expression to select children of   | 
      
| System.Xml.XmlNode | node | The child node to sort.  | 
      
| Func<System.Xml.XmlNode, System.Int32> | orderBy | A function returning the value to order the nodes by.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Boolean | A value indicating whether sorting was needed.  | 
      
Remarks
Assuming all nodes but node are sorted, this will move the node to
the right position without moving all the nodes (as SortNodes would do) - should improve perfs.
SortNodes(XmlNode, String, Func<XmlNode, Int32>)
Sorts the children of a parentNode.
Declaration
public static void SortNodes(XmlNode parentNode, string childNodesXPath, Func<XmlNode, int> orderBy)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Xml.XmlNode | parentNode | The parent node.  | 
      
| System.String | childNodesXPath | An XPath expression to select children of   | 
      
| Func<System.Xml.XmlNode, System.Int32> | orderBy | A function returning the value to order the nodes by.  | 
      
TryCreateXPathDocument(String, out XPathDocument)
Tries to create a new XPathDocument from an xml string.
Declaration
public static bool TryCreateXPathDocument(string xml, out XPathDocument doc)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.String | xml | The xml string.  | 
      
| System.Xml.XPath.XPathDocument | doc | The XPath document.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Boolean | A value indicating whether it has been possible to create the document.  | 
      
TryCreateXPathDocumentFromPropertyValue(Object, out XPathDocument)
Tries to create a new XPathDocument from a property value.
Declaration
public static bool TryCreateXPathDocumentFromPropertyValue(object value, out XPathDocument doc)
  Parameters
| Type | Name | Description | 
|---|---|---|
| System.Object | value | The value of the property.  | 
      
| System.Xml.XPath.XPathDocument | doc | The XPath document.  | 
      
Returns
| Type | Description | 
|---|---|
| System.Boolean | A value indicating whether it has been possible to create the document.  | 
      
Remarks
The value can be anything... Performance-wise, this is bad.