Class XmlExtensions
Extension methods for xml objects
Inheritance
Namespace: Umbraco.Extensions
Assembly: Umbraco.Core.dll
Syntax
public static class XmlExtensions
Methods
View SourceAttributeValue<T>(XElement, string)
Gets an attribute value from an XML element, converted to the specified type.
Declaration
public static T? AttributeValue<T>(this XElement xml, string attributeName)
Parameters
| Type | Name | Description |
|---|---|---|
| XElement | xml | The XML element. |
| string | attributeName | The name of the attribute. |
Returns
| Type | Description |
|---|---|
| T | The attribute value converted to the specified type, or the default value if not found or conversion fails. |
Type Parameters
| Name | Description |
|---|---|
| T | The type to convert the attribute value to. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the xml parameter is null. |
AttributeValue<T>(XmlNode, string)
Gets an attribute value from an XML node, converted to the specified type.
Declaration
public static T? AttributeValue<T>(this XmlNode xml, string attributeName)
Parameters
| Type | Name | Description |
|---|---|---|
| XmlNode | xml | The XML node. |
| string | attributeName | The name of the attribute. |
Returns
| Type | Description |
|---|---|
| T | The attribute value converted to the specified type, or the default value if not found or conversion fails. |
Type Parameters
| Name | Description |
|---|---|
| T | The type to convert the attribute value to. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the xml parameter is null. |
GetXElement(XmlNode)
Converts an System.Xml.XmlNode to an System.Xml.Linq.XElement.
Declaration
public static XElement? GetXElement(this XmlNode node)
Parameters
| Type | Name | Description |
|---|---|---|
| XmlNode | node | The XML node to convert. |
Returns
| Type | Description |
|---|---|
| XElement | The converted XElement, or null if the node has no root element. |
GetXmlNode(XContainer)
Converts an System.Xml.Linq.XContainer to an System.Xml.XmlNode.
Declaration
public static XmlNode? GetXmlNode(this XContainer element)
Parameters
| Type | Name | Description |
|---|---|---|
| XContainer | element | The XContainer to convert. |
Returns
| Type | Description |
|---|---|
| XmlNode | The converted XmlNode, or null if conversion fails. |
GetXmlNode(XContainer, XmlDocument)
Converts an System.Xml.Linq.XContainer to an System.Xml.XmlNode and imports it into the specified document.
Declaration
public static XmlNode? GetXmlNode(this XContainer element, XmlDocument xmlDoc)
Parameters
| Type | Name | Description |
|---|---|---|
| XContainer | element | The XContainer to convert. |
| XmlDocument | xmlDoc | The XML document to import the node into. |
Returns
| Type | Description |
|---|---|
| XmlNode | The imported XmlNode, or null if conversion fails. |
HasAttribute(XmlAttributeCollection, string)
Determines whether the attribute collection contains an attribute with the specified name.
Declaration
public static bool HasAttribute(this XmlAttributeCollection attributes, string attributeName)
Parameters
| Type | Name | Description |
|---|---|---|
| XmlAttributeCollection | attributes | The XML attribute collection. |
| string | attributeName | The name of the attribute to check for. |
Returns
| Type | Description |
|---|---|
| bool |
|
RequiredAttributeValue<T>(XElement, string)
Gets the required attribute value from an XML element, converted to the specified type.
Declaration
public static T? RequiredAttributeValue<T>(this XElement xml, string attributeName)
Parameters
| Type | Name | Description |
|---|---|---|
| XElement | xml | The XML element. |
| string | attributeName | The name of the attribute. |
Returns
| Type | Description |
|---|---|
| T | The attribute value converted to the specified type. |
Type Parameters
| Name | Description |
|---|---|
| T | The type to convert the attribute value to. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the xml parameter is null. |
| InvalidOperationException | Thrown when the attribute is not found or cannot be converted. |
ToDataString(XElement)
Converts an System.Xml.Linq.XElement to a string while preserving the exact line endings in text values.
Declaration
public static string ToDataString(this XElement xml)
Parameters
| Type | Name | Description |
|---|---|---|
| XElement | xml | The XML element to convert. |
Returns
| Type | Description |
|---|---|
| string | A string representation of the XML element with preserved line endings. |
Remarks
This method exists because XElement.ToString() with SaveOptions changes line endings. When saving data, we want the exact characters to be preserved.
ToXDocument(XmlDocument)
Converts from an XmlDocument to an XDocument
Declaration
public static XDocument ToXDocument(this XmlDocument xmlDocument)
Parameters
| Type | Name | Description |
|---|---|---|
| XmlDocument | xmlDocument |
Returns
| Type | Description |
|---|---|
| XDocument |
ToXElement(XmlNode)
Converts from an XmlElement to an XElement
Declaration
public static XElement ToXElement(this XmlNode xmlElement)
Parameters
| Type | Name | Description |
|---|---|---|
| XmlNode | xmlElement |
Returns
| Type | Description |
|---|---|
| XElement |
ToXmlDocument(XDocument)
Converts from an XDocument to an XmlDocument
Declaration
public static XmlDocument ToXmlDocument(this XDocument xDocument)
Parameters
| Type | Name | Description |
|---|---|---|
| XDocument | xDocument |
Returns
| Type | Description |
|---|---|
| XmlDocument |
ToXmlElement(XContainer)
Converts from an System.Xml.Linq.XContainer to an System.Xml.XmlNode.
Declaration
public static XmlNode? ToXmlElement(this XContainer xElement)
Parameters
| Type | Name | Description |
|---|---|---|
| XContainer | xElement | The XContainer to convert. |
Returns
| Type | Description |
|---|---|
| XmlNode | The document element as an XmlNode, or null if the document has no root element. |