ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

GetValue

Simply returns the value property from an XmlNode whether it's null or not. Simplifies using XmlDocuments.

Source

public static string GetValue( this XmlNode node )
{
    if ( node != null )
        return node.Value;
    else
        return string.Empty;
}

Example

var xDoc = new XmlDocument();
xDoc.LoadXml(xml);

var xmlNode = xDoc.SelectSingleNode("//result/text()");
string result = xmlNode.GetValue();

// OR

string result = xDoc.SelectSingleNode("//result/text()").GetValue();

Author: Dave Thieben

Submitted on: 16 jun 2010

Language: C#

Type: System.Xml.XmlNode

Views: 3394