Class XmlHelper
The XmlHelper class contains general helper methods for working with xml in umbraco.
Inheritance
Namespace:System.Dynamic.ExpandoObject
Assembly:Umbraco.Core.dll
Syntax
public class XmlHelper
Methods
AddAttribute(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 |
---|---|
System.Collections.Generic.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 |
ImportXmlNodeFromText(String, ref XmlDocument)
Imports a XML node from text.
Declaration
public static XmlNode ImportXmlNodeFromText(string text, ref XmlDocument xmlDoc)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The text. |
System.Xml.XmlDocument | xmlDoc | The XML doc. |
Returns
Type | Description |
---|---|
System.Xml.XmlNode |
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)
Opens a file as a XmlDocument.
Declaration
public static XmlDocument OpenAsXmlDocument(string filePath)
Parameters
Type | Name | Description |
---|---|---|
System.String | filePath | The relative file path. ei. /config/umbraco.config |
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 |
Split(String, String[], String, String)
Splits the specified delimited string into an XML document.
Declaration
public static XmlDocument Split(string data, string[] separator, string rootName, string elementName)
Parameters
Type | Name | Description |
---|---|---|
System.String | data | The data. |
System.String[] | separator | The separator. |
System.String | rootName | Name of the root. |
System.String | elementName | Name of the element. |
Returns
Type | Description |
---|---|
System.Xml.XmlDocument | Returns an |
Split(XmlDocument, String, String[], String, String)
Splits the specified delimited string into an XML document.
Declaration
public static XmlDocument Split(XmlDocument xml, string data, string[] separator, string rootName, string elementName)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlDocument | xml | The XML document. |
System.String | data | The delimited string data. |
System.String[] | separator | The separator. |
System.String | rootName | Name of the root node. |
System.String | elementName | Name of the element node. |
Returns
Type | Description |
---|---|
System.Xml.XmlDocument | Returns an |
StripDashesInElementOrAttributeNames(String)
Declaration
public static string StripDashesInElementOrAttributeNames(string xml)
Parameters
Type | Name | Description |
---|---|---|
System.String | xml |
Returns
Type | Description |
---|---|
System.String |
TryCreateXElementFromPropertyValue(Object, out XElement)
Tries to create a new XElement
from a property value.
Declaration
public static bool TryCreateXElementFromPropertyValue(object value, out XElement elt)
Parameters
Type | Name | Description |
---|---|---|
System.Object | value | The value of the property. |
System.Xml.Linq.XElement | elt | The Xml element. |
Returns
Type | Description |
---|---|
System.Boolean | A value indicating whether it has been possible to create the element. |
Remarks
The value can be anything... Performance-wise, this is bad.
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.